为什么此反射不从程序集返回自定义属性?



我正在学习如何使用自定义属性将元信息添加到我的参数类。我正在遵循一本名为"专业c# 5.0"的教科书中的一个示例。

下面是嵌入在测试夹具中的完整程序。Assert语句应该返回一个大于0的值;但事实并非如此。我不知道为什么。请协助。下面的代码是自包含的:创建一个新的类库项目,并确保有一个对NUnit的引用来运行单元测试。另一方面,如果你是一个专家,你可能只是阅读代码,并给我反馈。

using System;
using System.Reflection;
using NUnit.Framework;
namespace GameDesigner.Sandbox.TestFixtures
{
    [TestFixture]
    internal class DeclarativeAttributesTestFixture
    {
        [Test]
        public void UseReflectionToFindNumericAttributes()
        {
            Assembly theAssembly = typeof (PhaseState).Assembly;
            Assert.AreEqual("GameDesigner.Sandbox, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
                            theAssembly.FullName);
            Attribute[] supportingAttributes = Attribute.GetCustomAttributes(theAssembly, typeof(OptimizableNumeric));
            Assert.IsTrue(supportingAttributes.Length > 0, "supportingAttributes was length: " + supportingAttributes.Length);
        }
    }

    public class PhaseState
    {
        public PhaseState(double temperatue, int pressure, string state)
        {
            Temperature = temperatue;
            Pressure = pressure;
            State = state;
        }
        [OptimizableNumeric(0.0, 300.0, 1.0)] public double Temperature;
        [OptimizableNumeric(1.0, 10.0, 1.0)] public int Pressure;
        [OptimizableNominal(new string[] {"solid", "liquid", "gas"})] public string State;
    }
    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
    public class OptimizableNumeric : Attribute
    {
        private readonly double _start;
        private readonly double _stop;
        private readonly double _stepSize;
        public OptimizableNumeric(double start, double stop, double stepSize)
        {
            _stepSize = stepSize;
            _stop = stop;
            _start = start;
        }
        public double Start
        {
            get { return _start; }
        }
        public double Stop
        {
            get { return _stop; }
        }
        public double StepSize
        {
            get { return _stepSize; }
        }
    }
    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
    public class OptimizableNominal : Attribute
    {
        private readonly string[] _nominalList;

        public OptimizableNominal(string[] nominalList)
        {
            _nominalList = nominalList;
        }
        public string[] NominalList
        {
            get { return _nominalList; }
        }
    }
}

我已经尝试了许多不同的如何检索自定义属性的例子,他们都没有产生结果。由于我是在不了解自己在做什么的情况下抄袭课本,所以代码对我来说很难诊断。

Attribute[] GetCustomAttributes(组件元素,Type attributeType)方法返回应用于组件的自定义属性数组。应用于程序集的属性类似于

[assembly: AssemblyCompany("Microsoft")]

您的属性未应用于程序集。使用以下代码获取应用于State字段的自定义属性:

var memberInfo = typeof(PhaseState).GetField("State");
Attribute[] supportingAttributes = 
  Attribute.GetCustomAttributes(memberInfo, typeof(OptimizableNominalAttribute));

如果要检查程序集中具有此属性的所有公共成员,可以使用以下查询:

var attributeType = typeof(OptimizableNominalAttribute);
var supportingAttributes = theAssembly.GetTypes()
          .SelectMany(t => t.GetMembers()) // you can pass binding flags here
          .SelectMany(m => Attribute.GetCustomAttributes(m, attributeType));

查询语法:

var supportingAttributes = 
      from t in theAssembly.GetTypes()
      from m in t.GetMembers()
      from a in Attribute.GetCustomAttributes(m, attributeType)
      select a;

相关内容

  • 没有找到相关文章

最新更新