快速动态访问属性
public static class EmitDynamicProperty { private static ConcurrentDictionary<string, Delegate> action = new ConcurrentDictionary<string, Delegate>(); public static TRet DynamicGetProperty<TRet>(this object obj, string propertyName) { var type = obj.GetType(); string key = string.Concat(type.FullName, "_", propertyName); var gater = action.GetOrAdd(key, k => { var method = type.GetProperty(propertyName).GetGetMethod(); var dynamicMethod = new DynamicMethod(string.Empty, typeof(TRet), new Type[] { typeof(object) }); var ilGen = dynamicMethod.GetILGenerator(); ilGen.Emit(OpCodes.Ldarg_0); ilGen.Emit(OpCodes.Castclass, type); ilGen.Emit(OpCodes.Callvirt, method); if (method.ReturnType.IsValueType && (!typeof(TRet).IsValueType)) { ilGen.Emit(OpCodes.Box, method.ReturnType); } ilGen.Emit(OpCodes.Ret); return dynamicMethod.CreateDelegate(typeof(Func<object, TRet>)); }); return ((Func<object, TRet>)gater)(obj); } }
QQ:273352165
evlon#126.com
转载请注明出处。