how to override setters when implementing Interface with properties using TypeBuilder C#

121 Views Asked by At

What I'm doing is creating a dynamic type from System.Data.DataTable through reflection. But i'm stuck when implementing an Interface to my dynamic type created by TypeBuilder.

My Interface looks like this

public interface ICustomViewCols
{
    int? CardId { get; set; }

    DateTime? OutDate { get; set; }

    string HospCode { get; set; }
}

And my TypeBuilder looks like

var aName = new AssemblyName("UniWeb.CustomView");
var ab =
    AssemblyBuilder.DefineDynamicAssembly(aName,
        AssemblyBuilderAccess.RunAndCollect);
var mb =
    ab.DefineDynamicModule(aName.Name + ".dll");

var tb = mb.DefineType(
    "CustomViewModel",
    TypeAttributes.Public);

tb.AddInterfaceImplementation(typeof(ICustomViewCols));

foreach (var obj in cols)
{
    var col = obj as DataColumn;
    var propType = GetNullableDataType(col.DataType);
    var propName = col.ColumnName;

    if (propName == "HospCode" || propName == "CardId" || propName == "OutDate")
    {
        var getMethod = typeof(ICustomViewCols).GetProperty(propName).GetGetMethod();
        var setMethod = typeof(ICustomViewCols).GetProperty(propName).GetSetMethod();

        var currGetPropMthdBldr = tb.DefineMethod($"get_{propName}",
            MethodAttributes.Public | MethodAttributes.Virtual,
            propType, Type.EmptyTypes);
        var getIl = currGetPropMthdBldr.GetILGenerator();
        getIl.Emit(OpCodes.Ret);
        tb.DefineMethodOverride(currGetPropMthdBldr, getMethod);

        var currSetPropMthdBldr = tb.DefineMethod($"set_{propName}",
            MethodAttributes.Public | MethodAttributes.Virtual,
            null, new[] { propType });
        var setIl = currGetPropMthdBldr.GetILGenerator();
        setIl.Emit(OpCodes.Ldarg_0);
        setIl.Emit(OpCodes.Ldc_I4_1);
        setIl.Emit(OpCodes.Ldarg_1);
        setIl.Emit(OpCodes.Callvirt, setMethod);

        setIl.Emit(OpCodes.Ret);
        tb.DefineMethodOverride(currSetPropMthdBldr, setMethod);
    }
    else
    {
        var field = tb.DefineField(propName, propType, FieldAttributes.Public);
        var property = tb.DefineProperty(propName, PropertyAttributes.None, propType,
            new Type[] {propType});
        var GetSetAttr = MethodAttributes.Public | MethodAttributes.HideBySig;
        var currGetPropMthdBldr =
            tb.DefineMethod($"get_{propName}", GetSetAttr, propType, new Type[] {propType}); // Type.EmptyTypes);
        var currGetIL = currGetPropMthdBldr.GetILGenerator();
        currGetIL.Emit(OpCodes.Ldarg_0);
        currGetIL.Emit(OpCodes.Ldfld, field);
        currGetIL.Emit(OpCodes.Ret);

        var currSetPropMthdBldr =
            tb.DefineMethod($"set_{propName}", GetSetAttr, null, new Type[] {propType});
        var currSetIL = currSetPropMthdBldr.GetILGenerator();
        currSetIL.Emit(OpCodes.Ldarg_0);
        currSetIL.Emit(OpCodes.Ldarg_1);
        currSetIL.Emit(OpCodes.Stfld, field);
        currSetIL.Emit(OpCodes.Ret);

        property.SetGetMethod(currGetPropMthdBldr);
        property.SetSetMethod(currSetPropMthdBldr);
    }
}
var customViewType = tb.CreateType();

And this works only for the getters in my interface but not work for the setters. It throws an exception of System.InvalidOperationException: Method 'set_HospCode' does not have a method body. Could anybody help me out?

1

There are 1 best solutions below

0
Boby Huang On

Here's the follow up for my question.

I changed my Interface with only getters.

public interface ICustomViewCols
{
    int Id { get; }

    DateTime? OutDate { get; }

    string HospCode { get; }
}


And then I just implement the getters of the interface and consider the setters as property setters.

    private Type CreateDynamicTypeFromDataColumn(IEnumerable<DataColumn> cols)
    {
        //naming
        var aName = new AssemblyName("UniWeb.CustomView");
        var ab =
            AssemblyBuilder.DefineDynamicAssembly(aName,
                AssemblyBuilderAccess.RunAndCollect);
        var mb =
            ab.DefineDynamicModule(aName.Name + ".dll");

        var tb = mb.DefineType(
            "CustomViewModel",
            TypeAttributes.Public);

        tb.AddInterfaceImplementation(typeof(ICustomViewCols));

        foreach (var col in cols)
        {
            var propName = col.ColumnName;
            var propType = propName == nameof(ICustomViewCols.Id)
                ? col.DataType
                : GetNullableDataType(col.DataType);

            var field = tb.DefineField(propName, propType, FieldAttributes.Public);
            var property = tb.DefineProperty(propName, PropertyAttributes.None, propType,
                new Type[] {propType});
            var propertyAttr = MethodAttributes.Public | MethodAttributes.HideBySig;

            //implement interface getter
            if (propName == nameof(ICustomViewCols.HospCode)
                || propName == nameof(ICustomViewCols.Id)
                || propName == nameof(ICustomViewCols.OutDate))
            {
                var getMethod = typeof(ICustomViewCols).GetProperty(propName).GetGetMethod();

                var currGetPropMthdBldr = tb.DefineMethod($"get_{propName}",
                    MethodAttributes.Public | MethodAttributes.Virtual,
                    propType, Type.EmptyTypes);
                var getIl = currGetPropMthdBldr.GetILGenerator();
                getIl.Emit(OpCodes.Ret);
                tb.DefineMethodOverride(currGetPropMthdBldr, getMethod);
            }

            //set property getter
            else
            {
                var currGetPropMthdBldr =
                    tb.DefineMethod($"get_{propName}", propertyAttr, propType,
                        new Type[] {propType});
                var currGetIL = currGetPropMthdBldr.GetILGenerator();
                currGetIL.Emit(OpCodes.Ldarg_0);
                currGetIL.Emit(OpCodes.Ldfld, field);
                currGetIL.Emit(OpCodes.Ret);
                property.SetGetMethod(currGetPropMthdBldr);
            }

            //set property setter
            var currSetPropMthdBldr =
                tb.DefineMethod($"set_{propName}", propertyAttr, null, new Type[] {propType});
            var currSetIL = currSetPropMthdBldr.GetILGenerator();
            currSetIL.Emit(OpCodes.Ldarg_0);
            currSetIL.Emit(OpCodes.Ldarg_1);
            currSetIL.Emit(OpCodes.Stfld, field);
            currSetIL.Emit(OpCodes.Ret);

            property.SetSetMethod(currSetPropMthdBldr);
        }

        return tb.CreateType();
    }

And it works.