Hello, I got a following base class:
public class ValidationItem
{
public ObservableCollection<object> GetFilteredValues( ObservableCollection<object> values)
{
return new ObservableCollection<object>(); // nothing here yet
}
}
I create a type which inherits this base type and I create a getter which is going to return a base class GetFilteredValues method result.
This is how a new property should look like:
public ObservableCollection<object> Values
{
get { return GetFilteredValues(_values); }
set { _values = value; }
}
This is what I do:
Type pType = typeof(ObservableCollection<object>);
FieldBuilder fieldBuilder = tb.DefineField("_" + propertyName, pType, FieldAttributes.Private);
PropertyBuilder propertyBuilder = tb.DefineProperty( propertyName, PropertyAttributes.HasDefault, pType, null);
MethodBuilder getPropMthdBldr = tb.DefineMethod("get_" + propertyName,
MethodAttributes.Public |
MethodAttributes.SpecialName |
MethodAttributes.HideBySig,
pType, Type.EmptyTypes);
getPropMthdBldr.SetReturnType(typeof(ObservableCollection<>).MakeGenericType(typeof(object)));
ILGenerator getIL = getPropMthdBldr.GetILGenerator();
MethodInfo minfo = typeof(ValidationItem).GetMethod("GetFilteredValues", new[] { typeof(ObservableCollection<object>) }); // it's not null so everything is ok here
getIL.Emit(OpCodes.Ldarg_0);
getIL.Emit(OpCodes.Ldfld, fieldBuilder);
getIL.EmitCall(OpCodes.Callvirt, minfo, Type.EmptyTypes);
getIL.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(getPropMthdBldr);
But each time I run an app and use this created type, I get an error "Common Language Runtime detected an invalid program". What am I doing wrong?
Thanks in advance.