使用反射在运行时创建的类型的属性上添加属性

时间:2015-10-30 18:11:13

标签: c# reflection reflection.emit

我正在尝试在运行时创建一个类型,在我在此类型上添加的每个属性上都附加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;
    }

Property Builder:

    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。如您所见,我正在尝试在每个GetCustomAttributes()元素上调用PropertyInfo属性。

[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();

我做错了什么?

1 个答案:

答案 0 :(得分:0)

它已经解决了:

我创建了StuckAttribute作为内部类。我已经解决了它将访问者类设置为public

所以,我的测试现在运行:

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

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