从动态生成的类中获取属性

fre*_*rik 5 c# c#-4.0

我遇到了一个问题,这个问题通常无法解决看似相同的问题.

考虑:

我有一组动态生成的类,继承自已知的基类(让我们调用它BaseClass).这些动态生成的类也具有动态生成Properties的关联属性.

属性也是自定义类,但不是动态生成的:

[AttributeUsage(AttributeTargets.Property)]
class TypeAttribute: Attribute
{
    private Type _type;
    public Type Type
    {
        get { return _type; }
    }

    public TypeAttribute(Type t)
    {
        _type = t;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我想,运行时当然是获取这个赋值属性的值:

List<PropertyInfo> result = target.GetType()
  .GetProperties()
  .Where(
     p =>
        p.GetCustomAttributes(typeof(TypeAttribute), true)
        //.Where(ca => ((TypeAttribute)ca).)
        .Any()
     )
  .ToList();
Run Code Online (Sandbox Code Playgroud)

where target是的子类BaseClass.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);
Run Code Online (Sandbox Code Playgroud)

其中dataType是在属性存储的类型和tbTypeBuilder为类.

如果我getCustomAttributes()在该属性上做,我得到预期的属性,除了我正在寻找的属性.但是,如果我这样做,getCustomAttributesData()我得到所有这些,但我正在寻找的是类型CustomAttributeData,并且不能转换为TypeAttribute(如果我检查VS调试器中的实例,我可以看到包含的信息是为a TypeAttribute).我猜这是问题的症状,但我找不到原因 - 更不用说解决方案了.

任何人都可以向我指出为什么result列表是空的?

Mar*_*ell 2

对我来说效果很好;您确定您的属性和 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Attribute.IsDefined另外,如果您只关心它是否存在,那么使用起来会更有效。

List<PropertyInfo> result = target.GetType().GetProperties()
      .Where(p => Attribute.IsDefined(p, typeof(TypeAttribute), true)).ToList();
Run Code Online (Sandbox Code Playgroud)

另请注意,我必须欺骗这些getType(dataType)东西 - 我看不到它在做什么,因为它不在问题中。确保它返回一个Type.