Reflection Emit for Property Getter

1.5k views Asked by At

I want to build type dynamically like this:

public class Sample
{
    Sample Parent { get; set; }
    public Sample(Sample parent)
    {
        Parent = parent;
    }

    public int Depth
    {
        get
        {
            if (Parent == null)
                return -1;
            else
                return Parent.Depth + 1;
        }
    }
}

The code I write is :

        const string assemblyName = "SampleAssembly";
        const string parentPproperty = "Parent";
        const string depthProperty = "Depth";
        const string typeName = "Sample";     
        const string assemblyFileName = assemblyName + ".dll";

        AppDomain domain = AppDomain.CurrentDomain;
        AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.RunAndSave);
        ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName, assemblyFileName);
        TypeBuilder typeBuilder = moduleBuilder.DefineType(typeName, TypeAttributes.Public);
        FieldBuilder parentField = typeBuilder.DefineField($"_{parentPproperty}", typeBuilder, FieldAttributes.Private);
        PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(parentPproperty, PropertyAttributes.None, parentField.FieldType, Type.EmptyTypes);
        MethodAttributes getSetAttr = MethodAttributes.Public | 
        MethodAttributes.SpecialName | MethodAttributes.HideBySig;

        MethodBuilder getParentMethod = typeBuilder.DefineMethod($"get_{propertyBuilder.Name}", getSetAttr, parentField.FieldType, Type.EmptyTypes);
        ILGenerator il = getParentMethod.GetILGenerator();
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Ldfld, parentField);
        il.Emit(OpCodes.Ret);
        propertyBuilder.SetGetMethod(getParentMethod);

        MethodBuilder setParentMethod = typeBuilder.DefineMethod($"set_{propertyBuilder.Name}", qetSetAttr, null, Type.EmptyTypes);
        il = setParentMethod.GetILGenerator();
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Ldarg_1);
        il.Emit(OpCodes.Stfld, parentField);
        il.Emit(OpCodes.Ret);
        propertyBuilder.SetSetMethod(setParentMethod);


        parentField = typeBuilder.DefineField($"_{depthProperty}", typeBuilder, FieldAttributes.Private);
        propertyBuilder = typeBuilder.DefineProperty(depthProperty, PropertyAttributes.None, parentField.FieldType, Type.EmptyTypes);
        MethodBuilder getDepthMethod = typeBuilder.DefineMethod($"get_{depthProperty}", getSetAttr , parentField.FieldType, Type.EmptyTypes);
        il = getDepthMethod.GetILGenerator();
        LocalBuilder lb = il.DeclareLocal(typeof(bool));

        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Call, getParentMethod);
        il.Emit(OpCodes.Ldnull);
        il.Emit(OpCodes.Ceq);
        il.Emit(OpCodes.Stloc_0);
        il.Emit(OpCodes.Ldloc_0);
        il.Emit(OpCodes.Brfalse_S);
        il.Emit(OpCodes.Ldc_I4_1);
        il.Emit(OpCodes.Stloc_1);
        il.Emit(OpCodes.Br_S);
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Call, getParentMethod);
        il.Emit(OpCodes.Callvirt, getDepthMethod);
        il.Emit(OpCodes.Ldc_I4_1);
        il.Emit(OpCodes.Add);
        il.Emit(OpCodes.Stloc_1);
        il.Emit(OpCodes.Br_S);
        il.Emit(OpCodes.Ldloc_1);
        il.Emit(OpCodes.Ret);
        propertyBuilder.SetGetMethod(getDepthMethod);

        ConstructorBuilder constructor = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new Type[] { typeBuilder });
        il= constructor.GetILGenerator();            
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Ldarg_1);
        il.Emit(OpCodes.Call, setParentMethod);          
        il.Emit(OpCodes.Ret);

        Type type = typeBuilder.CreateType();
        var obj1 = Activator.CreateInstance(type, null);

        var obj2 = Activator.CreateInstance(type, obj1);

        assemblyBuilder.Save(assemblyFileName);

I think I had problem in building constructor and Depth property getter method.

Please help me get out of this.

Instance is not created as well.

Thanks

1

There are 1 answers

1
thehennyy On BEST ANSWER

There are a few issues in your code.

In your parent setter method, you missed to declare the parameter:

MethodBuilder setParentMethod = typeBuilder.DefineMethod($"set_{propertyBuilder.Name}", getSetAttr, null, new [] { propertyBuilder.PropertyType });

You are declaring a redundant backing field here, just delete this line:

parentField = typeBuilder.DefineField($"_{depthProperty}", typeBuilder, FieldAttributes.Private);

Your depth property is of the wrong type, it has to be of type int:

propertyBuilder = typeBuilder.DefineProperty(depthProperty, PropertyAttributes.None, typeof(int), Type.EmptyTypes);
MethodBuilder getDepthMethod = typeBuilder.DefineMethod($"get_{depthProperty}", getSetAttr, propertyBuilder.PropertyType, Type.EmptyTypes);

The IL code you are generating for your computed property seems like debugging code, I replaced it with release code. Also, you are emitting incomplete branching instructions, you should pass a target label as second parameter, have a look at this working method body:

il = getDepthMethod.GetILGenerator();
        
var notNullLabel = il.DefineLabel();

il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, getParentMethod);
il.Emit(OpCodes.Brtrue_S, notNullLabel);
il.Emit(OpCodes.Ldc_I4_M1);
il.Emit(OpCodes.Ret);
il.MarkLabel(notNullLabel);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, getParentMethod);
il.Emit(OpCodes.Callvirt, getDepthMethod);
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Ret);

Last but not least, you have to wrap your null argument in an object array so that the activator can find the expected constructor overload:

var obj1 = Activator.CreateInstance(type, new object[] { null });