反射類:測試
using System; using System.Reflection.Emit; using System.Reflection; public sealed class DynamicMethodCompiler { // DynamicMethodCompiler private DynamicMethodCompiler() { } // CreateGetDelegate internal static Func<T, F> CreateGetHandler<T, F>(Type type, FieldInfo fieldInfo) { DynamicMethod dynamicGet = CreateGetDynamicMethod<T, F>(type); ILGenerator getGenerator = dynamicGet.GetILGenerator(); getGenerator.Emit(OpCodes.Ldarg_0); getGenerator.Emit(OpCodes.Ldfld, fieldInfo); getGenerator.Emit(OpCodes.Ret); return (Func<T, F>)dynamicGet.CreateDelegate(typeof(Func<T, F>)); } // CreateSetDelegate internal static Action<T, F> CreateSetHandler<T, F>(Type type, FieldInfo fieldInfo) { DynamicMethod dynamicSet = CreateSetDynamicMethod<T, F>(type); ILGenerator setGenerator = dynamicSet.GetILGenerator(); setGenerator.Emit(OpCodes.Ldarg_0); setGenerator.Emit(OpCodes.Ldarg_1); setGenerator.Emit(OpCodes.Stfld, fieldInfo); setGenerator.Emit(OpCodes.Ret); return (Action<T, F>)dynamicSet.CreateDelegate(typeof(Action<T, F>)); } // CreateGetDelegate internal static Func<T, F> CreateGetHandler<T, F>(Type type, PropertyInfo propertyInfo) { MethodInfo getMethodInfo = propertyInfo.GetGetMethod(true); DynamicMethod dynamicGet = CreateGetDynamicMethod<T, F>(type); ILGenerator getGenerator = dynamicGet.GetILGenerator(); getGenerator.Emit(OpCodes.Ldarg_0); getGenerator.Emit(OpCodes.Call, getMethodInfo); getGenerator.Emit(OpCodes.Ret); return (Func<T, F>)dynamicGet.CreateDelegate(typeof(Func<T, F>)); } // CreateSetDelegate internal static Action<T, F> CreateSetHandler<T, F>(Type type, PropertyInfo propertyInfo) { MethodInfo setMethodInfo = propertyInfo.GetSetMethod(true); DynamicMethod dynamicSet = CreateSetDynamicMethod<T, F>(type); ILGenerator setGenerator = dynamicSet.GetILGenerator(); setGenerator.Emit(OpCodes.Ldarg_0); setGenerator.Emit(OpCodes.Ldarg_1); setGenerator.Emit(OpCodes.Call, setMethodInfo); setGenerator.Emit(OpCodes.Ret); return (Action<T, F>)dynamicSet.CreateDelegate(typeof(Action<T, F>)); } // CreateGetDynamicMethod private static DynamicMethod CreateGetDynamicMethod<T, F>(Type type) { return new DynamicMethod("DynamicGet", typeof(F), new Type[] { typeof(T) }, type, true); } // CreateSetDynamicMethod private static DynamicMethod CreateSetDynamicMethod<T, F>(Type type) { return new DynamicMethod("DynamicSet", typeof(void), new Type[] { typeof(T), typeof(F) }, type, true); } }
業務類:spa
class PuOrder { private string __orderCode; public string OrderCode { get { return __orderCode; } set { __orderCode = value; } } }
測試方法:code
PuOrder order = new PuOrder(); Type type = typeof(PuOrder); PropertyInfo propertyInfo = type.GetProperty("OrderCode"); Func<PuOrder, string> getPropertyInfo = DynamicMethodCompiler.CreateGetHandler<PuOrder, string>(type, propertyInfo); Action<PuOrder, string> setPropertyInfo = DynamicMethodCompiler.CreateSetHandler<PuOrder, string>(type, propertyInfo); setPropertyInfo(order, "000000001"); string currentValue = getPropertyInfo(order);