【问题标题】:Best practice instantiating generic delegates and accessing property getters实例化泛型委托和访问属性获取器的最佳实践
【发布时间】:2025-12-19 12:00:11
【问题描述】:

我想创建委托来访问不同对象的属性,而无需事先知道它们。

我有以下定义

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

有没有更好的方法

  1. 创建属性吸气剂?
  2. 枚举支持的硬编码为字符串的属性类型?

编辑:

我关心的是性能,因为这些代表会被频繁调用(滴答场景),所以任何强制转换都会减慢它的速度。虽然需要更优雅的解决方案,但性能仍然是我的主要关注点

我在此处的 Code Review 上发布了相同的问题,因此考虑到there 的回复,我将其标记为已解决

【问题讨论】:

    标签: c# .net generics delegates properties


    【解决方案1】:

    这可以在 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();
       }
    

    【讨论】:

    • 那么我将如何调用这个委托来获取我的对象 obj 的属性值?
    • 感谢代码审查建议。我试试看
    • @anchandra:我添加了更多信息。如果这个例子不清楚,请告诉我,我可能会看看编写实现,因为我可能在某些时候也需要它。 :)
    • @anchandra:不要介意这个例子,jim31415 的答案已经有一个针对 getter 的特定实现,这将导致相同的结果。您的实现确实稍微提高了性能,但灵活性要差得多。我认为您应该只在注意到需要时才担心性能。
    • 感谢您的更新。然而,性能是我主要关心的问题,因为我希望每秒处理大量数据。
    【解决方案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 );
    

    【讨论】:

    • 我尝试了这里描述的方法,但是在性能方面,它比我现在这样做的效率低
    【解决方案3】:

    更好的方法:

    1. 使用Expression&lt;TDelegate&gt;。例如:

      公共静态类 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. 要获取类型中所有属性的可枚举类型,请执行以下操作:typeof(T).GetProperties().Select(x=&gt;x.PropertyType).Distinct();

    查看某人为 C# TypeSwitch 编写的源代码,可在this post 中找到。我想它可能有你要找的东西。

    【讨论】:

    • 大约 2)。我不关心如何获取属性列表,我想知道是否有比使用带有硬编码类型名称的“switch”语句更好的方法
    • 更新了我的答案 - 看看最后。