【问题标题】:How to dynamically use the PropertyType reflection attribute to create a respective typed Function<>?如何动态使用 PropertyType 反射属性来创建相应的类型化 Function<>?
【发布时间】:2010-05-06 03:43:19
【问题描述】:

我想使用 PropertyType 返回的类型来创建类型化函数。我发现这个类似 using type returned by Type.GetType() in c# 但这提到了如何创建一个列表,但没有提到我们如何创建一个 Func。请帮帮我。

伪代码:

PropertyInfo inf = typeof(SomeClass).GetProperty("PropertyName");
Type T=inf.PropertyType;
Expression<Func<SomeClass,T>> le = GetPropertyOrFieldByName<SomeClass,T>("PropertyName");

static Expression<Func<TSource, TResult>> GetPropertyOrFieldByName<TSource,TResult>(string propertyOrFieldName)
{ 
ParameterExpression item = Expression.Parameter(typeof(TSource), "expr");MemberExpression prop = LambdaExpression.PropertyOrField(item, propertyOrFieldName);
var expr = Expression.Lambda<Func<TSource, TResult>>(prop, new ParameterExpression[] { item });
expr.Compile();
return expr;
}

【问题讨论】:

  • 你能展示一些你想要实现的伪代码吗?你的问题有点含糊。
  • 你想创建委托实例还是什么?
  • 对不起,如果有点含糊...这里有一些代码..这是我想做的... PropertyInfo inf = typeof(SomeClass).GetProperty("PropertyName");类型 T=inf.PropertyType;表达式> le = GetPropertyOrFieldByName("PropertyName");
  • @vsj 好的,但是你打算以后用它做什么?您将无法正常使用它
  • @Andrey ..好吧,我有这个 GetPropertyOrFieldByName Func 它获取类的那个字段......稍后使用它我得到相应字段的值......所以基本上如果我知道我可以将它传递给该函数的类型...

标签: c# reflection gettype


【解决方案1】:

如果您想用PropertyType 调用GetPropertyOrFieldByName,这应该可以:

PropertyInfo inf = typeof(SomeClass).GetProperty("PropertyName");
Type T = inf.PropertyType;

object le =
    typeof([TypeThatDeclaresMethod]).GetMethod("GetPropertyOrFieldByName")
    .MakeGenericMethod(typeof(SomeClass), T)
    .Invoke(null, new object[] { "PropertyName" });

假设GetPropertyOrFieldByName方法是一个公共静态方法。

【讨论】:

  • 您好,我尝试使用上述方法,但得到一个空引用异常...我想从该方法中获取一个列表,TypeThatDeclaresMethod 表示具有该方法或返回类型的类?对不起,我我是反射新手..所以我已将 GetPropertyOrFieldByName 添加到问题中的上述代码中..请帮帮我
  • [TypeThatDeclaresMethod] 是具有该方法的类的名称。因此,如果您有一个名为 Program 的类包含 GetPropertyOrFieldByName,那么您的调用将是:typeof(Program).GetMethod("GetPropertyOrFieldByName")
  • 嗨,谢谢..实际上我没有公共方法,所以它返回 null 现在我有一个具有表达式值的 Object 变量,所以我将它转换为字符串,所以我如何使用这个字符串变量作为一个表达式?
  • 您能重新表述一下您的问题吗?我不明白你想知道什么。
  • 好吧,我现在有一个字符串,它具有要评估的表达式值..就像 expr=>expr.FieldName 所以我想将此字符串用作 Linq.Expression 或任何其他方式来查询数据库.
【解决方案2】:

只需使用MakeGenericType(new Type[] { SomeClass, T })

编辑更多细节:

Type T2 = typeof(Excpression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(new Type[] { SomeClass, T }));
... Activator.CreateInstance(T2);

,

【讨论】:

    【解决方案3】:
    Type parameter = ...;
    Type result = ...;
    Type your_type = typeof(Func<,>).MakeGenericType(new Type[] {parameter, result});
    

    【讨论】:

      【解决方案4】:

      您可以创建表达式树,但不能在代码中声明其类型。你在哪里得到这个:

      Expression<Func<SomeClass,T>> le = ...
      

      T 必须在编译时知道(或者是一个类型参数)。相反,您的T 是一个变量,其值仅在执行时间 时才知道。

      现在,下一个问题是您是否真的需要它 - 或者您需要它的方式。你想用这个表达做什么?您可以只使用非泛型 Expression 类作为变量类型吗?

      如果你真的想要一个表达式树,你不需要担心MakeGenericType 等 - 使用Expression.Property 方法。

      【讨论】:

        猜你喜欢
        • 2010-11-21
        • 2018-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多