最佳实践:实例化泛型委托并访问属性获取器

5
我希望创建委托以访问不事先知道的不同对象的属性。
我有以下定义。
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可以是十进制数、字符串、日期时间或整数

我有一些初始化代码,将基于对象的反射属性创建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);
    }
}

有没有更好的方法来创建属性getter?

  1. 以硬编码为字符串的方式枚举支持的属性类型?

编辑:

我的担忧是性能,因为这些委托将经常被调用(滴答声场景),所以任何强制转换都会减慢速度。虽然更优雅的解决方案是可取的,但性能仍然是我的主要关注点。

我在Code Review上发布了相同的问题,所以我会将其标记为已解决,考虑到那里的回应。

3个回答

3

这是可以发布在代码审查上的内容,实际上我已经发布了一个类似的问题。我相信我的方法使用表达式树已经改进了你的方法。

使用示例:

Action<object> compatibleExecute =
    DelegateHelper.CreateCompatibleDelegate<Action<object>>( owner, method );

必要时会进行转换。传递给此函数的方法可以具有任何类型的参数。

更新:

我没有测试过这个,但在您的情况下,您可以尝试以下操作:

Func<object> getter =
    DelegateHelper.CreateCompatibleDelegate<Func<object>>( owner, method );

method 必须设置为您检索到的 getter。 owner 必须设置为您对象的实例。如果您想允许将 owner 作为委托的参数传递,则需要调整代码。Vladimir Matveev 在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();
   }

那么我该如何调用这个委托来获取对象 obj 的属性值呢? - anchandra
感谢您的代码审查建议。我会尝试一下。 - anchandra
@anchandra:我添加了更多信息。如果这个例子不清楚,请告诉我,我可能会考虑编写实现,因为在某些时候我也可能需要它。 :) - Steven Jeuris
@anchandra:不要在意这个例子,jim31415的答案已经有了一个特定的getter实现,会得到相同的结果。你的实现确实稍微更加高效,但是灵活性却大大降低了。我认为你应该只在注意到需要时才担心性能。 - Steven Jeuris
感谢更新。然而性能是我的主要关注点,因为我预计每秒需要处理大量数据。 - anchandra

2

更好的方式:

  1. Use Expression<TDelegate>. For example:

    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);
    }
    

    }

  2. To get an enumerable of the types of all the properties in a type, do this: typeof(T).GetProperties().Select(x=>x.PropertyType).Distinct();

看看有人为C# TypeSwitch编写的源代码,在这篇文章中可用。我认为它可能是你正在寻找的。


关于2)。我不关心如何获取属性列表,我想知道是否有比使用硬编码类型名称的“switch”语句更好的方法。 - anchandra
更新了我的回答 - 请看结尾。 - smartcaveman

2

请查看Jon Skeet写的这篇文章:

http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx

该方法动态确定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 );

我尝试了这里描述的方法,但就性能而言,它比我现在使用的方式效率低。 - anchandra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接