我想创建委托来访问不同对象的属性而不事先知道它们。
我有以下定义
public delegate T MyMethod<K, T>(K data);
public static MyMethod<K, T> CreatePropertyGetter<K, T>(PropertyInfo property)
{
MethodInfo mi = property.DeclaringType.GetMethod("get_" + property.Name);
return (MyMethod<K, T>)Delegate.CreateDelegate(typeof(MyMethod<K, T>), mi);
}
其中T可以是decimal,string,datetime或int
我有一些初始化代码,它将根据我对象的反射属性创建MyMethod委托,如下所示:
foreach (PropertyInfo property in entityType.GetProperties())
{
switch (property.PropertyType.Name)
{
case "System.Decimal":
return CreatePropertyGetter<T, decimal>(property);
case "System.DateTime":
return CreatePropertyGetter<T, DateTime>(property);
case "System.String":
return CreatePropertyGetter<T, DateTime>(property);
}
}
有没有更好的方法
修改
我关注的是性能,因为这些代表会被频繁调用(滴答场景),因此任何强制转换都会降低速度。虽然需要更优雅的解决方案,但性能仍然是我主要关心的问题
我在此处针对代码审核发布了相同的问题,因此我会将此标记为已解决,并考虑到回复there
答案 0 :(得分:3)
这可以发布在Code Review上,实际上,我已经发布了a similar question。我相信我使用表达式树的方法已经改进了你的方法。
使用示例:
Action<object> compatibleExecute =
DelegateHelper.CreateCompatibleDelegate<Action<object>>( owner, method );
必要时进行转化。传递给函数的方法可以包含任何类型的参数。
更新:
我没有对此进行测试,但在您的情况下,您可以尝试以下方法:
Func<object> getter =
DelegateHelper.CreateCompatibleDelegate<Func<object>>( owner, method );
必须将 method
设置为您检索到的getter。必须将owner
设置为对象的实例。如果要允许将所有者作为参数传递给委托,则必须调整代码。 Vladimir Matveev就the article of Jon Skeet的评论作了一个例子。
static Func<T, object, object> MagicMethod<T>(MethodInfo method)
{
var parameter = method.GetParameters().Single();
var instance = Expression.Parameter(typeof (T), "instance");
var argument = Expression.Parameter(typeof (object), "argument");
var methodCall = Expression.Call(
instance,
method,
Expression.Convert(argument, parameter.ParameterType)
);
return Expression.Lambda<Func<T, object, object>>(
Expression.Convert(methodCall, typeof (object)),
instance, argument
).Compile();
}
答案 1 :(得分:2)
使用Expression<TDelegate>
。例如:
public static class PropertyExpressionHelper {
public static TProperty GetProperty<T,TProperty>(this T obj, Expression<Func<T,TProperty>> getPropertyExpression)
{
if(obj == null)
{
throw new ArgumentNullException("obj");
}
if(getPropertyExpression==null)
{
throw new ArgumentNullException("getPropertyExpression");
}
var memberExpression = getPropertyExpression.Body as MemberExpression;
bool memberExpressionIsInvalidProperty = memberExpression == null ||
!(memberExpression.Member is PropertyInfo &&
memberExpression.Expression.Type == typeof (T));
if(memberExpressionIsInvalidProperty)
{
throw new ArgumentNullException("getPropertyExpression", "Not a valid property expression.");
}
return (TProperty)(memberExpression.Member as PropertyInfo).GetValue(obj, null);
}
}
要获取类型中所有属性的可枚举类型,请执行以下操作:typeof(T).GetProperties().Select(x=>x.PropertyType).Distinct();
查看有人为C#TypeSwitch编写的源代码,可在this post中找到。我想它可能有你想要的东西。
答案 2 :(得分:2)
查看Jon Skeet的这篇文章:
此方法动态确定getter的返回类型。
public static class DelegateCreator
{
//-----------------------------------------------------------------------
public static Func<T, object> GetMethod<T>( PropertyInfo propertyInfo )
where T : class
{
MethodInfo method = propertyInfo.GetGetMethod( true );
if( method == null )
{
string msg = String.Format( "Property '{0}' does not have a getter.", propertyInfo.Name );
throw new Exception( msg );
}
// First fetch the generic form
MethodInfo genericHelper = typeof( DelegateCreator ).GetMethod( "CreateGetHelper",
BindingFlags.Static | BindingFlags.NonPublic );
// Now supply the type arguments
MethodInfo constructedHelper = genericHelper.MakeGenericMethod
( typeof( T ), method.ReturnType );
// Now call it. The null argument is because it's a static method.
object ret = constructedHelper.Invoke( null, new object[] { method } );
// Cast the result to the right kind of delegate and return it
return (Func<T, object>)ret;
}
//-----------------------------------------------------------------------
static Func<TTarget, object> CreateGetHelper<TTarget, TReturn>( MethodInfo method )
where TTarget : class
{
// Convert the slow MethodInfo into a fast, strongly typed, open delegate
Func<TTarget, TReturn> func = (Func<TTarget, TReturn>)Delegate.CreateDelegate
( typeof( Func<TTarget, TReturn> ), method );
// Now create a more weakly typed delegate which will call the strongly typed one
Func<TTarget, object> ret = ( TTarget target ) => func( target );
return ret;
}
}
像这样使用:
PropertyInfo pi = typeof( Employee ).GetProperty( "LastName" );
Action<Employee, object> getMethod = DelegateCreator.SetMethod<Employee>( pi );
string lastName = getMethod( employee );