在我的解决方案中的 MiddleTier 项目中,我有 Customer 类,其中一个用 Attribute1 定义
public class Customer2
{
public string Name2 { get; set; }
public int Age2 { get; set; }
}
[MyAttribute1]
public class Customer1
{
[MyAttribute1(DefaultValue = "Must Enter Name")]
public string Name { get; set; }
[MyAttribute1(DefaultValue = "Must Enter Age")]
public int Age { get; set; }
}
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute1 : Attribute
{
public string DefaultValue { get; set; }
}
在一个单独的项目中,我引用了 MiddleTier DLL,我想枚举此 DLL 中的所有类并识别与 Attribute1 关联的 Customer1 类。
Assembly assembly = Assembly.LoadFrom(@"C:\myfolder\MiddleTier\bin\Debug\MiddleTier.dll");
foreach (Type type in assembly.GetTypes())
{
var attribs = type.GetCustomAttributes(typeof(MyAttribute1), false); <--- problem
if (attribs != null && attribs.Length > 0)
{
....
}
}
GetCustomAttributes调用没有返回任何属性。我究竟做错了什么?请帮忙。谢谢