簡體   English   中英

使用反射在運行時創建的類型的屬性上添加屬性

[英]Add attribute on property of a runtime created type using reflection

我試圖在運行時創建一個類型,在我為此類型添加的每個屬性上添加一個StuckAttribute屬性。

類型生成器:

private TypeBuilder getTypeBuilder()
    {
        var typeSignature = "IDynamicFlattenedType";
        var an = new AssemblyName(typeSignature);

        AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
        ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicDomain");
        TypeBuilder tb = moduleBuilder.DefineType(typeSignature
                            , TypeAttributes.Public |
                            TypeAttributes.Interface) |
                            TypeAttributes.Abstract |
                            TypeAttributes.AutoClass |
                            TypeAttributes.AnsiClass
                            , null);

        return tb;
    }

物業開發商:

    private void createProperty(TypeBuilder tb, string propertyName, Type propertyType)
    {
        Type[] ctorParams = new Type[] { typeof(string) };
        ConstructorInfo classCtorInfo = typeof(StuckAttribute).GetConstructor(ctorParams);

        CustomAttributeBuilder myCABuilder2 = new CustomAttributeBuilder(
                            classCtorInfo,
                            new object[] { DateTime.Now.ToString() });

        PropertyBuilder propertyBuilder = tb.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null);
        propertyBuilder.SetCustomAttribute(myCABuilder2);

        MethodBuilder getPropMthdBldr = tb.DefineMethod("get_" + propertyName,
            MethodAttributes.Public |
            MethodAttributes.Abstract |
            MethodAttributes.Virtual |
            MethodAttributes.HideBySig |
            MethodAttributes.NewSlot,
            CallingConventions.HasThis,
            propertyType,
            Type.EmptyTypes
        );
        getPropMthdBldr.SetImplementationFlags(MethodImplAttributes.Managed);

        MethodBuilder setPropMthdBldr =
            tb.DefineMethod("set_" + propertyName,
                MethodAttributes.Public |
                MethodAttributes.Abstract |
                MethodAttributes.Virtual |
                MethodAttributes.HideBySig |
                MethodAttributes.NewSlot,
                CallingConventions.HasThis,
                null, new[] { propertyType });
        setPropMthdBldr.SetImplementationFlags(MethodImplAttributes.Managed);

        propertyBuilder.SetGetMethod(getPropMthdBldr);
        propertyBuilder.SetSetMethod(setPropMthdBldr);
    }

我創建了一個簡單的測試,以檢查StuckAttribute是否在屬性上。 如您所見,我試圖在每個PropertyInfo元素上獲取屬性調用GetCustomAttributes()

[Test]
public void test()
{
    Type flattenedType = Reflection.Classes.FlattenClassBuilder.flattenType<TestClass>(this.classes);

    flattenedType.Should().NotBeNull();

    PropertyInfo[] properties = flattenedType.GetProperties();
    properties.Should().NotBeEmpty().And.HaveCount(4);

    IEnumerable<Attribute> attrs = properties[0].GetCustomAttributes();
    attrs.Should().NotBeEmpty();
}

但是它失敗了。 最后一個斷言失敗:

 attrs.Should().NotBeEmpty();

我究竟做錯了什么?

解決了:

我創建了StuckAttribute作為內部類。 我已經解決了將Accesor類設置為public

因此,我的測試現在運行:

PropertyInfo[] properties = flattenedType.GetProperties();
properties.Should().NotBeEmpty().And.HaveCount(4);

properties.Should().OnlyContain(p => p.GetCustomAttribute<Reflection.Classes.FieldPropertyOwnerAttribute>() != null);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM