dimanche 13 décembre 2015

Reflection.Emit. Override a property with an internal setter

I have a class like this:

public class ParentClass
{
    public virtual int Property { get; internal set; }
}

I want to use a TypeBuilder to create a class which looks like this:

public class ChildClass : ParentClass
{
    public override int Property
    {
        get
        {
            return 0;
        }
    }
}

However, I am getting the following error when building my type:

An unhandled exception of type 'System.TypeLoadException' occurred in mscorlib.dll Additional information: Method 'set_Property' on type 'ChildClass' from assembly 'Proxies, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is overriding a method that is not visible from that assembly.

My code looks like this:

var property = typeBuilder.DefineProperty("Property",
    PropertyAttributes.None, typeof(int), null);

var getMethod = typeBuilder.DefineMethod("get_Property",
    MethodAttributes.HideBySig | MethodAttributes.Virtual | 
    MethodAttributes.Public | MethodAttributes.SpecialName, 
    typeof(int), Type.EmptyTypes);

var body = getMethod.GetILGenerator();
// do IL generation here

property.SetGetMethod(getMethod);

From the error it looks like I am attempting to set a set_Method, but in code I am not. Has anyone any ideas on this?





Aucun commentaire:

Enregistrer un commentaire