我如何构建这个 C#“表达式”在运行时使用反射?

发布于 2024-12-21 12:43:52 字数 1264 浏览 3 评论 0原文

直到今天,我还没有找到一篇关于表达式的好文章 - 以及如何查看 C# lambda 语句并说“哦,那是废话”...所以,如果您知道一篇好文章,我将不胜感激这也是一个答案。

解释问题的代码示例

所以...给出以下 c# 代码:

public class SomeClass<T>
{
    public TResult SomeMethod<TResult>(Expression<Func<T, TResult>> expression)
    {
        // This is just an example... don't get hung up on this :)
        return default(TResult);
    }
}

public class Person
{
    public string FirstName { get; set; }

    public string LastName { get; set; }
}

我如何

var blah = new SomeClass<Person>();

blah.SomeMethod(p => p.FirstName);

在运行时执行此操作(使用反射)?

我所期望的答案

我有点期待这样的东西......但我确信我对表达方式的选择还很遥远。

// By the way, these values are being passed to me... so you
// can't change this part of the question :)
Type personType = typeof(Person);
string propertyName = "FirstName";

// THIS CODE BELOW IS OBVIOUSLY WRONG, BUT YOU GET THE IDEA OF
// WHAT I HOPE TO DO... THIS LINE OF CODE BELOW IS **ALL** I'M
// ASKING HOW TO DO :)
var expression = Expression.MakeUnary(ExpressionType.Lambda,
    Expression.Property(Expression.Parameter(personType, "p"),
    propertyName), typeof(string));

blah.SomeMethod(expression);

To this day, I have not found a great article about expressions - and how to look at a C# lambda statement and say "oh, that's a blah blah"... so, if you know of a good article, I'd appreciate that as an answer too.

Code sample to explain the question

So... given the following c# code:

public class SomeClass<T>
{
    public TResult SomeMethod<TResult>(Expression<Func<T, TResult>> expression)
    {
        // This is just an example... don't get hung up on this :)
        return default(TResult);
    }
}

public class Person
{
    public string FirstName { get; set; }

    public string LastName { get; set; }
}

How do I do this...

var blah = new SomeClass<Person>();

blah.SomeMethod(p => p.FirstName);

at runtime (using reflection)?

What I expect as an answer

I kinda expect something like this... but I'm sure I'm way off with my choice of expressions.

// By the way, these values are being passed to me... so you
// can't change this part of the question :)
Type personType = typeof(Person);
string propertyName = "FirstName";

// THIS CODE BELOW IS OBVIOUSLY WRONG, BUT YOU GET THE IDEA OF
// WHAT I HOPE TO DO... THIS LINE OF CODE BELOW IS **ALL** I'M
// ASKING HOW TO DO :)
var expression = Expression.MakeUnary(ExpressionType.Lambda,
    Expression.Property(Expression.Parameter(personType, "p"),
    propertyName), typeof(string));

blah.SomeMethod(expression);

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

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

发布评论

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

评论(2

|煩躁 2024-12-28 12:43:52

试试这个:

var functorParam = Expression.Parameter(typeof(Person));
var lambda = Expression.Lambda(
    Expression.Property(functorParam, typeof(Person).GetProperty("FirstName"))
,   functorParam /* <<= EDIT #1 */
);
blah.SomeMethod((Expression<Func<Person,string>>)lambda); /* EDIT #2 */

Try this:

var functorParam = Expression.Parameter(typeof(Person));
var lambda = Expression.Lambda(
    Expression.Property(functorParam, typeof(Person).GetProperty("FirstName"))
,   functorParam /* <<= EDIT #1 */
);
blah.SomeMethod((Expression<Func<Person,string>>)lambda); /* EDIT #2 */
春庭雪 2024-12-28 12:43:52

ExpressionBuilder 是必经之路。

ExpressionBuilder is the way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文