N
none
I'd like to create a new static property in a class "hiding" the
property present in a base class. Since this needs to happen at runtime
I tried doing this via DynamicMethod. But obviously the created methods
are not "registered" and only available through the DynamicMethod class.
So a method lookup finds the origin property.
A little test:
public class DerivedClass : BaseClass
{
public static DynamicMethod CustomMethod;
}
public class BaseClass
{
public static int Something
{
get
{
return 5;
}
}
public static void Main(string[] args)
{
DerivedClass.CustomMethod = new
System.Reflection.Emit.DynamicMethod("get_Something",
MethodAttributes.NewSlot | MethodAttributes.Static |
MethodAttributes.Public, CallingConventions.Standard, typeof(int), null,
typeof(DerivedClass), false);
ILGenerator generator = DerivedClass.CustomMethod.GetILGenerator();
generator.Emit(OpCodes.Ldc_I4, 3);
generator.Emit(OpCodes.Ret);
Console.WriteLine("BaseClass: " + Something.ToString() + "
DerivedClass: " + DerivedClass.Something.ToString());
}
}
Currently gives BaseClass: 5 DerivedClass: 5.
I'd like it to give me BaseClass: 5 DerivedClass: 3.
(How) can this be achived?
Regards
LCID Fire
property present in a base class. Since this needs to happen at runtime
I tried doing this via DynamicMethod. But obviously the created methods
are not "registered" and only available through the DynamicMethod class.
So a method lookup finds the origin property.
A little test:
public class DerivedClass : BaseClass
{
public static DynamicMethod CustomMethod;
}
public class BaseClass
{
public static int Something
{
get
{
return 5;
}
}
public static void Main(string[] args)
{
DerivedClass.CustomMethod = new
System.Reflection.Emit.DynamicMethod("get_Something",
MethodAttributes.NewSlot | MethodAttributes.Static |
MethodAttributes.Public, CallingConventions.Standard, typeof(int), null,
typeof(DerivedClass), false);
ILGenerator generator = DerivedClass.CustomMethod.GetILGenerator();
generator.Emit(OpCodes.Ldc_I4, 3);
generator.Emit(OpCodes.Ret);
Console.WriteLine("BaseClass: " + Something.ToString() + "
DerivedClass: " + DerivedClass.Something.ToString());
}
}
Currently gives BaseClass: 5 DerivedClass: 5.
I'd like it to give me BaseClass: 5 DerivedClass: 3.
(How) can this be achived?
Regards
LCID Fire