Properly emit property

483 views Asked by At

I'm writing an interface implementator for simple interfaces like

interface IPoint 
{
    int X { get; }
    int Y { get; }
}

It almost works, but when I try to implement any property I get an error

Signature of the body and declaration in a method implementation do not match

I don't understand why Emit thinks that properties does not match.

Here is code sample:

private static class InterfaceImplementator<T> where T: class 
{
    [SuppressMessage("ReSharper", "StaticMemberInGenericType")]
    public static Type Value { get; }
    static InterfaceImplementator()
    {
        var interfaceType = typeof(T);
        if (!interfaceType.IsInterface)
        {
            throw new ArgumentException($"{interfaceType.FullName} should be an interface!");
        }
        var interfaceProps = interfaceType.GetProperties();
        if (interfaceType.GetMethods().Except(interfaceProps.Select(x => x.GetMethod).Concat(interfaceProps.Select(x => x.SetMethod))).Any())
        {
            throw new ArgumentException($"{interfaceType.FullName} must have properties only!");
        }
        var tb = Builder.DefineType($"<{interfaceType.Name}>__Implementation", TypeAttributes.Class | TypeAttributes.Sealed);
        foreach (var interfaceProp in interfaceProps)
        {
            var prop = tb.EmitAutoProperty(interfaceProp.Name, interfaceProp.PropertyType);
            if (interfaceProp.CanRead)
            {
                tb.DefineMethodOverride(prop.GetMethod, interfaceProp.GetMethod);
            }
            if (interfaceProp.CanWrite)
            {
                tb.DefineMethodOverride(prop.SetMethod, interfaceProp.SetMethod);
            }
        }
        tb.AddInterfaceImplementation(interfaceType);
        Value = tb.CreateType();
    }
}

where EmitProperty:

public static PropertyInfo EmitAutoProperty(this TypeBuilder tb, string propertyName, Type propertyType)
{
    var backingField = tb.DefineField($"<{propertyName}>k__BackingField", propertyType, FieldAttributes.Private);
    var propertyBuilder = tb.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null);

    var getMethod = tb.DefineMethod("get_" + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig);
    var getGenerator = getMethod.GetILGenerator();
    getGenerator.Emit(OpCodes.Ldarg_0);
    getGenerator.Emit(OpCodes.Ldfld, backingField);
    getGenerator.Emit(OpCodes.Ret);
    propertyBuilder.SetGetMethod(getMethod);

    var setMethod = tb.DefineMethod("set_" + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig);
    var setGenerator = setMethod.GetILGenerator();
    setGenerator.Emit(OpCodes.Ldarg_0);
    setGenerator.Emit(OpCodes.Ldarg_1);
    setGenerator.Emit(OpCodes.Stfld, backingField);
    setGenerator.Emit(OpCodes.Ret);
    propertyBuilder.SetSetMethod(setMethod);

    return propertyBuilder;
}
1

There are 1 answers

1
Graeme Wicksted On BEST ANSWER

Try using the 4-arg DefineMethod call for the get_ and set_ methods so you can define the return type / arg:

var getMethod = tb.DefineMethod("get_" + propertyName,
                                   MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,        
                                   propertyType,
                                   Type.EmptyTypes);


var setMethod = tb.DefineMethod("set_" + propertyName,
                                   MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,        
                                   null,
                                   new [] { propertyType });