public static PropertyGetter EmitGetPropertyValueDelegate(this PropertyInfo pi) { var dynMeth = CreateDynamicMethod( "__SI_IL_get__" + pi.Name, pi.DeclaringType, typeof(object), new Type[] { typeof(object) }); var IL = new IL(dynMeth.GetILGenerator()); bool isStatic = pi.IsStatic(); if (!isStatic) { IL .ldarg_0 // load arg-0 (this) .castclass(pi.DeclaringType); // (PropertyOwnerType)this } var getMethod = pi.GetGetMethod(nonPublic: true); IL .call_or_callvirt(getMethod) .box_if_value_type(pi.PropertyType) .ret(); return (PropertyGetter)dynMeth.CreateDelegate(typeof(PropertyGetter)); }
public static PropertySetter EmitSetPropertyValueDelegate(this PropertyInfo pi) { var dynMeth = CreateDynamicMethod( "__SI_IL_set__" + pi.Name, pi.DeclaringType, null, new Type[] { typeof(object), typeof(object) }); var IL = new IL(dynMeth.GetILGenerator()); IL .ldarg_0 .castclass(pi.DeclaringType) .ldarg_1 .end(); if (pi.PropertyType.IsClass) { IL .castclass(pi.PropertyType); } else IL .cast_object_to(pi.PropertyType); var setMethod = pi.GetSetMethod(nonPublic: true); IL .call_or_callvirt(setMethod) .ret(); //dynMeth.DefineParameter(1, ParameterAttributes.In, "property_owner_instance"); //dynMeth.DefineParameter(2, ParameterAttributes.In, "property_value"); return (PropertySetter)dynMeth.CreateDelegate(typeof(PropertySetter)); }