Search code examples
c#reflectionreflection.emit

Add attribute on property of a runtime created type using reflection


I'm trying to create a type at runtime sticking a StuckAttribute attribute on every property I add on this type.

Type Builder:

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);
    }

I've created a simple test in order to check the StuckAttribute is on the properties. As you can see, I'm trying to get the attributes call GetCustomAttributes() over each PropertyInfo element.

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

However it fails. It fails onthe last assert:

 attrs.Should().NotBeEmpty();

What am I doing wrong?


Solution

  • It's been solved:

    I had created StuckAttribute as an internal class. I've settled it setting the accesor class as public.

    So, my test runs now:

    PropertyInfo[] properties = flattenedType.GetProperties();
    properties.Should().NotBeEmpty().And.HaveCount(4);
    
    properties.Should().OnlyContain(p => p.GetCustomAttribute<Reflection.Classes.FieldPropertyOwnerAttribute>() != null);