我遇到了一个小问题,对于看似相同的问题,普遍可用的解决方案并没有解决。
考虑:
我有一组动态生成的类,继承自一个已知的基类(我们称之为 BaseClass
)。
这些动态生成的类还具有动态生成的 Properties
和相关属性。
属性也是一个自定义类,虽然不是动态生成的:
[AttributeUsage(AttributeTargets.Property)]
class TypeAttribute: Attribute
{
private Type _type;
public Type Type
{
get { return _type; }
}
public TypeAttribute(Type t)
{
_type = t;
}
}
然后我想,当然是运行时,获取这个指定属性的值:
List<PropertyInfo> result = target.GetType()
.GetProperties()
.Where(
p =>
p.GetCustomAttributes(typeof(TypeAttribute), true)
//.Where(ca => ((TypeAttribute)ca).)
.Any()
)
.ToList();
其中 target
是 BaseClass
的子类。然而,List result
是空的,这让我很困惑。
我添加属性使用
PropertyBuilder propertyBuilder = tb.DefineProperty(propertyName,
PropertyAttributes.HasDefault, propertyType, null);
ConstructorInfo classCtorInfo = typeof(TypeAttribute).
GetConstructor(new Type[] { typeof(Type) });
CustomAttributeBuilder myCABuilder = new CustomAttributeBuilder(
classCtorInfo, new object[] { getType(dataType) });
propertyBuilder.SetCustomAttribute(myCABuilder);
其中 dataType
是要存储在属性中的类型,tb
是类的 TypeBuilder
。
如果我对属性执行 getCustomAttributes()
,我会得到预期的属性,但我正在寻找的属性除外。但是如果我执行 getCustomAttributesData()
我会得到所有这些,但我正在寻找的是类型 CustomAttributesData
并且不可转换为 TypeAttribute
(如果我在 VS 调试器中检查实例,我可以看到包含的信息是针对 TypeAttribute
的)。
我猜这是问题的症状,但我找不到原因 - 更不用说解决方案了。
谁能告诉我为什么 result
列表是空的?
最佳答案
对我来说很好用;您确定您的属性和 getter 或 setter 方法足够存在以至于它实际上显示为属性吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
[AttributeUsage(AttributeTargets.Property)]
public class TypeAttribute : Attribute
{
private Type _type;
public Type Type
{
get { return _type; }
}
public TypeAttribute(Type t)
{
_type = t;
}
}
public class BaseClass
{
}
static class Program
{
static void Main()
{
var aName = new AssemblyName("MyAssembly");
var ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
aName, AssemblyBuilderAccess.RunAndSave);
var mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");
var tb = mb.DefineType("MyType", TypeAttributes.Public, typeof(BaseClass));
var propertyName = "MyProperty";
var propertyType = typeof(int);
var propertyBuilder = tb.DefineProperty(propertyName,
PropertyAttributes.HasDefault, propertyType, null);
var classCtorInfo = typeof(TypeAttribute).
GetConstructor(new Type[] { typeof(Type) });
Type tArg = typeof(float); // for no real reason
var myCABuilder = new CustomAttributeBuilder(
classCtorInfo, new object[] { tArg });
propertyBuilder.SetCustomAttribute(myCABuilder);
var field = tb.DefineField("myField", propertyType, FieldAttributes.Private);
var getter = tb.DefineMethod("get_" + propertyName,
MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Public,
propertyType, Type.EmptyTypes);
propertyBuilder.SetGetMethod(getter);
var il = getter.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, field);
il.Emit(OpCodes.Ret);
var setter = tb.DefineMethod("set_" + propertyName,
MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Public,
typeof(void), new[] { typeof(int) });
il = setter.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Stfld, field);
il.Emit(OpCodes.Ret);
propertyBuilder.SetSetMethod(setter);
var target = Activator.CreateInstance(tb.CreateType());
List<PropertyInfo> result = target.GetType()
.GetProperties()
.Where(
p =>
p.GetCustomAttributes(typeof(TypeAttribute), true)
//.Where(ca => ((TypeAttribute)ca).)
.Any()
).ToList();
}
}
此外,如果您只关心它是否退出,使用 Attribute.IsDefined
会更有效。
List<PropertyInfo> result = target.GetType().GetProperties()
.Where(p => Attribute.IsDefined(p, typeof(TypeAttribute), true)).ToList();
另请注意,我不得不欺骗 getType(dataType)
东西 - 我看不到它在做什么,因为它不在问题中。确保它返回一个 Type
。
关于c# - 从动态生成的类中获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20100505/