I have problem in generating getterMethod for HasChildren Property in following code.
Could someone help me out of it?
Code in c#
public class Sample
{
public ObservableCollection<Sample> Children { get; set; }
public bool HasChildren { get { return Children?.Count() > 0; } }
}
Code which is not working is quoted here for methodbuilder.
const string assemblyName = "HasChildrenAssembly";
const string childrenProperty = "Children";
const string hasChildrenProperty = "HasChildren";
const string typeName = "Sample";
const string assemblyFileName = assemblyName + ".dll";
AppDomain domain = AppDomain.CurrentDomain;
AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.RunAndSave);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName, assemblyFileName);
TypeBuilder typeBuilder = moduleBuilder.DefineType(typeName, TypeAttributes.Public);
Type typeOfChildren = typeof(ObservableCollection<>);
Type genericTypeOfChildren = typeOfChildren.MakeGenericType(typeBuilder);
FieldBuilder childrenField = typeBuilder.DefineField($"_{childrenProperty}", genericTypeOfChildren, FieldAttributes.Private);
PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(childrenProperty, PropertyAttributes.None, childrenField.FieldType, Type.EmptyTypes);
MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
MethodBuilder getChildrenMethod = typeBuilder.DefineMethod($"get_{propertyBuilder.Name}", getSetAttr, childrenField.FieldType, Type.EmptyTypes);
ILGenerator il = getChildrenMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, childrenField);
il.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(getChildrenMethod);
MethodBuilder setChildrenMethod = typeBuilder.DefineMethod($"set_{propertyBuilder.Name}", getSetAttr, null, new[] { propertyBuilder.PropertyType });
il = setChildrenMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Stfld, childrenField);
il.Emit(OpCodes.Ret);
propertyBuilder.SetSetMethod(setChildrenMethod);
MethodInfo countMethodInfo = typeof(System.Linq.Enumerable).GetMethods().Single(method => method.Name == "Count" && method.IsStatic && method.GetParameters().Length == 1);
propertyBuilder = typeBuilder.DefineProperty(hasChildrenProperty, PropertyAttributes.None, typeof(bool), Type.EmptyTypes);
MethodBuilder getHasChildrenMethod = typeBuilder.DefineMethod($"get_{hasChildrenProperty}", getSetAttr, propertyBuilder.PropertyType, Type.EmptyTypes);
il = getHasChildrenMethod.GetILGenerator();
var notNullLabel = il.DefineLabel();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, getChildrenMethod);
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Brtrue_S, notNullLabel);
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Ret);
il.MarkLabel(notNullLabel);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, countMethodInfo);
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Cgt);
il.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(getHasChildrenMethod);
ConstructorBuilder constructor = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new Type[] { typeBuilder });
il = constructor.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Call, setChildrenMethod);
il.Emit(OpCodes.Ret);
Type type = typeBuilder.CreateType();
var obj1 = Activator.CreateInstance(type, new object[] { null });
var obj2 = Activator.CreateInstance(type, obj1);
assemblyBuilder.Save(assemblyFileName);
Thanks in advance.
You don't specify generic arguments in the
Count
method.You must specify that generic argument is a
Sample
by using MakeGenericMethodYour also have an extra
Ldarg_0
right after thenotNullLabel
.this
is already on stack at this point, thanks to thedup
operation at the very beginning.Here is a slightly modified version wich works as you expect :