尝试创建两个发出的委托词典,以便在动态获取/设置属性值时提高性能。
代码:
Properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanRead && !p.GetIndexParameters().Any())
.AsEnumerable();
PropertyGetters = Properties.ToDictionary(p => p.Name, p => (Func<object, object>)Delegate.CreateDelegate(typeof(Func<object, object>), p.GetGetMethod()));
PropertySetters = Properties.Where(p => p.GetSetMethod() != null)
.ToDictionary(p => p.Name, p => (Action<object, object>)Delegate.CreateDelegate(typeof(Action<object, object>), p.GetSetMethod()));
但是我得到以下例外:
无法绑定到目标方法,因为它的签名或安全性 透明度与委托类型的透明度不兼容。
根据我的阅读,这将由静态/索引/值类型属性引起,Properties
集合不包含静态或索引属性,但我确实需要它来处理值类型属性,如{{ 1}}和int
。
如何在保持代码抽象和避免泛型的同时创建我需要的getter / setter?
答案 0 :(得分:7)
好的,最后从这个问题中找到答案: MethodInfo.Invoke performance issue
更具体地说,这篇文章: Making reflection fly and exploring delegates
以下是我最终得到的代码的主旨:
public class Helper
{
private IDictionary<string, Func<object, object>> PropertyGetters { get; set; }
private IDictionary<string, Action<object, object>> PropertySetters { get; set; }
public static Func<object, object> CreateGetter(PropertyInfo property)
{
if (property == null)
throw new ArgumentNullException("property");
var getter = property.GetGetMethod();
if (getter == null)
throw new ArgumentException("The specified property does not have a public accessor.");
var genericMethod = typeof(Helper).GetMethod("CreateGetterGeneric");
MethodInfo genericHelper = genericMethod.MakeGenericMethod(property.DeclaringType, property.PropertyType);
return (Func<object, object>)genericHelper.Invoke(null, new object[] { getter });
}
public static Func<object, object> CreateGetterGeneric<T, R>(MethodInfo getter) where T : class
{
Func<T, R> getterTypedDelegate = (Func<T, R>)Delegate.CreateDelegate(typeof(Func<T, R>), getter);
Func<object, object> getterDelegate = (Func<object, object>)((object instance) => getterTypedDelegate((T)instance));
return getterDelegate;
}
public static Action<object, object> CreateSetter(PropertyInfo property)
{
if (property == null)
throw new ArgumentNullException("property");
var setter = property.GetSetMethod();
if (setter == null)
throw new ArgumentException("The specified property does not have a public setter.");
var genericMethod = typeof(Helper).GetMethod("CreateSetterGeneric");
MethodInfo genericHelper = genericMethod.MakeGenericMethod(property.DeclaringType, property.PropertyType);
return (Action<object, object>)genericHelper.Invoke(null, new object[] { setter });
}
public static Action<object, object> CreateSetterGeneric<T, V>(MethodInfo setter) where T : class
{
Action<T, V> setterTypedDelegate = (Action<T, V>)Delegate.CreateDelegate(typeof(Action<T, V>), setter);
Action<object, object> setterDelegate = (Action<object, object>)((object instance, object value) => { setterTypedDelegate((T)instance, (V)value); });
return setterDelegate;
}
public Helper(Type type)
{
var Properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanRead && !p.GetIndexParameters().Any()).AsEnumerable();
PropertyGetters = Properties.ToDictionary(p => p.Name, p => CreateGetter(p));
PropertySetters = Properties.Where(p => p.GetSetMethod() != null)
.ToDictionary(p => p.Name, p => CreateSetter(p));
}
}
生成的委托平均比使用反射快80%,所以我对结果感到满意!
答案 1 :(得分:-1)
我得到同样的错误。我使用 Expressions API 来解决此问题。
注意:要引用的方法是
委托名称是公式,其签名如下
public delegate float Formula(Dictionary<string, float> cr,
List<Dictionary<string, float>> allr);
获取将被引用为Delegate
的MethodInfoAssembly assembly = results.CompiledAssembly;
var generatedType = assembly.GetType("First.NewClass");
var generatedMethod = generatedType.GetMethod("FormulaMethod");
准备作为参数表达式的委托的参数。
论点1:Dictionary<string, float>
参数2:List<Dictionary<string, float>>
var arg1Expression = Expression.Parameter(typeof(Dictionary<string, float>));
var arg2Expression = Expression.Parameter(typeof(List&gt;));
生成最终方法Call Expression并返回Delegate。
var methodCall = Expression.Call(generatedMethod,
arg1Expression,
arg2Expression);
return Expression.Lambda <Formula> (methodCall,
arg1Expression,
arg2Expression).Compile();