private static FastSetValueHandler SetValueInvoker(FieldInfo field) { //定义一个没有名字的动态方法 DynamicMethod dynamicMethod = new DynamicMethod(String.Empty, null, new Type[] { typeof(Object), typeof(Object) }, field.DeclaringType.Module, true); ILGenerator il = dynamicMethod.GetILGenerator(); EmitHelper help = new EmitHelper(il); // 必须考虑对象是值类型的情况,需要拆箱 // 其它地方看到的程序从来都没有人处理 // 值类型是不支持这样子赋值的,暂时没有找到更好的替代方法 help.Ldarg(0) .CastFromObject(field.DeclaringType) .Ldarg(1); //il.Emit(OpCodes.Ldarg_0); //help.CastFromObject(field.DeclaringType); //il.Emit(OpCodes.Ldarg_1); MethodInfo method = GetMethod(field.FieldType); if (method != null) { //// 使用Convert.ToInt32(value) //il.EmitCall(OpCodes.Call, method, null); help.Call(method); } else { //if (field.FieldType.IsValueType) // il.Emit(OpCodes.Unbox_Any, field.FieldType); //else // il.Emit(OpCodes.Castclass, field.FieldType); help.CastFromObject(field.FieldType); } il.Emit(OpCodes.Stfld, field); il.Emit(OpCodes.Ret); return((FastSetValueHandler)dynamicMethod.CreateDelegate(typeof(FastSetValueHandler))); }
private static FastGetValueHandler GetValueInvoker(MethodInfo method) { //定义一个没有名字的动态方法 DynamicMethod dynamicMethod = new DynamicMethod(String.Empty, typeof(Object), new Type[] { typeof(Object) }, method.DeclaringType.Module, true); ILGenerator il = dynamicMethod.GetILGenerator(); EmitHelper help = new EmitHelper(il); //if (!method.IsStatic) il.Emit(OpCodes.Ldarg_0); if (!method.IsStatic) { help.Ldarg(0).CastFromObject(method.DeclaringType); } // 目标方法没有参数 help.Call(method) .BoxIfValueType(method.ReturnType) .Ret(); return((FastGetValueHandler)dynamicMethod.CreateDelegate(typeof(FastGetValueHandler))); }
static void GetMethodInvoker(ILGenerator il, MethodInfo method) { EmitHelper help = new EmitHelper(il); Type retType = method.ReturnType; //if (!method.IsStatic) il.Emit(OpCodes.Ldarg_0); if (!method.IsStatic) { help.Ldarg(0).CastFromObject(method.DeclaringType); } // 方法的参数数组放在动态方法的第二位,所以是1 help.PushParams(1, method) .Call(method) .BoxIfValueType(retType); //处理返回值,如果调用的方法没有返回值,则需要返回一个空 if (retType == null || retType == typeof(void)) { help.Ldnull().Ret(); } else { help.Ret(); } //调用目标方法 //if (method.IsVirtual) // il.EmitCall(OpCodes.Callvirt, method, null); //else // il.EmitCall(OpCodes.Call, method, null); ////处理返回值 //if (method.ReturnType == typeof(void)) // il.Emit(OpCodes.Ldnull); //else if (method.ReturnType.IsValueType) // il.Emit(OpCodes.Box, method.ReturnType); //il.Emit(OpCodes.Ret); }
private static FastHandler GetConstructorInvoker(Type target, ConstructorInfo constructor) { // 定义一个没有名字的动态方法。 // 关联到模块,并且跳过JIT可见性检查,可以访问所有类型的所有成员 DynamicMethod dynamicMethod = new DynamicMethod(String.Empty, typeof(Object), new Type[] { typeof(Object[]) }, target.Module, true); { ILGenerator il = dynamicMethod.GetILGenerator(); EmitHelper help = new EmitHelper(il); if (target.IsValueType) { help.NewValueType(target).BoxIfValueType(target).Ret(); } else if (target.IsArray) { help.PushParams(0, new Type[] { typeof(Int32) }).NewArray(target.GetElementType()).Ret(); } else { help.PushParams(0, constructor).NewObj(constructor).Ret(); } } #if DEBUG //SaveIL(dynamicMethod, delegate(ILGenerator il) // { // EmitHelper help = new EmitHelper(il); // if (target.IsValueType) // help.NewValueType(target).BoxIfValueType(target).Ret(); // else if (target.IsArray) // help.PushParams(0, new Type[] { typeof(Int32) }).NewArray(target.GetElementType()).Ret(); // else // help.PushParams(0, constructor).NewObj(constructor).Ret(); // }); #endif return((FastHandler)dynamicMethod.CreateDelegate(typeof(FastHandler))); }