当我只有泛型类型和实例值时,如何手动构建表达式树?

发布于 2024-12-19 11:12:28 字数 571 浏览 1 评论 0原文

我有一个如下所示的通用类:

public class MyClass<T, TProperty>
{
     MyClass(Expression<Func<T, TProperty>> expression)
     {
     }
}

就我而言,我想使用 Activator 动态创建该类的实例。所以我需要的是为构造函数创建一个表达式。我拥有的是类型(System.Type)和对象(System.Object),我想要如下所示的内容:

Expression<Func<T, TProperty>> exp = x => someValue;

“someValue”被声明为对象,但它的真实类型绝对是TProperty。它是通过反射解决的,所以这里的类型是object。

问题是类型 T 和 TProperty 将是通用的,我直到运行时才知道类型,所以我无法将“someValue”转换为 TProperty。我们拥有的是 typeof(T)、typeof(TProperty) 和一个对象 -,-

I have a generic class like below:

public class MyClass<T, TProperty>
{
     MyClass(Expression<Func<T, TProperty>> expression)
     {
     }
}

In my case, I want to dynamically create an instance of that class using Activator. So what I need is to create an expression for the constructor. What I have are the types (System.Type) and an object (System.Object) and I want something like below:

Expression<Func<T, TProperty>> exp = x => someValue;

"someValue" is declared as object but it's real type is definitely TProperty. It's resolved by reflection so the type here is object.

The problem is that the type T and TProperty will be generic, I don't know the types until run-time so I can't cast "someValue" to TProperty. What we have are typeof(T), typeof(TProperty) and an object -,-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有一腔孤勇 2024-12-26 11:12:28

好吧,我想我明白这个问题了。

给定示例输入:

Type typeOfT = typeof(int);
Type typeOfTProperty = typeof(string);
object someValue = "Test";

您想要创建相当于以下内容的代码:

var myClassInstance = new MyClass<int, string>(t => "Test");

以下是您可以做到这一点的方法。

首先,创建表达式树:

var parameter = Expression.Parameter(typeOfT, "t");
var body = Expression.Constant(someValue, typeOfTProperty);

// Will automatically be an Expression<Func<T, TProperty>> without any extra effort.
var lambda = Expression.Lambda(body, parameter);

然后使用反射创建 MyClass 实例:

var myClassType = typeof(MyClass<,>).MakeGenericType(typeOfT, typeOfTProperty);

//  Make the MyClass<,> constructor public first...
var myClassInstance = Activator.CreateInstance(myClassType, lambda);

Ok, I think I understand the question.

Given sample input:

Type typeOfT = typeof(int);
Type typeOfTProperty = typeof(string);
object someValue = "Test";

You want to create code equivalent to:

var myClassInstance = new MyClass<int, string>(t => "Test");

Here's how you can do that.

First, create the expression-tree:

var parameter = Expression.Parameter(typeOfT, "t");
var body = Expression.Constant(someValue, typeOfTProperty);

// Will automatically be an Expression<Func<T, TProperty>> without any extra effort.
var lambda = Expression.Lambda(body, parameter);

And then use reflection to create the MyClass<T, TProperty> instance:

var myClassType = typeof(MyClass<,>).MakeGenericType(typeOfT, typeOfTProperty);

//  Make the MyClass<,> constructor public first...
var myClassInstance = Activator.CreateInstance(myClassType, lambda);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文