从表达式函数获取父属性 [英] Get parent property from Expression function

查看:67
本文介绍了从表达式函数获取父属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,假设我有以下课程:

So let's say that I have the following classes:

public class Model {
    public AnotherModel InnerModel {
        get;
        set;
    }
}

public class AnotherModel {
    public String Value{
        get;
        set;
    }
}

现在我具有以下功能:

public static void Foo<T, U>(Expression<Func<T, U>> func) {
     // Get the property info from func
}

我现在想做的是以下事情:

What I would like to do now is the following:

Foo<Model, String>(o => o.InnerModel.Value)

问题来了:

我知道您可以通过执行以下操作从表达式func中获取PropertyInfo:

I know that you can fetch the PropertyInfo from the expression func by doing:

PropertyInfo propertyInfo = (PropertyInfo)((MemberExpression)func.Body).Member;

这将为我获取Value属性的PropertyInfo.但是,我也想获取有关父属性(即InnerModel属性)的信息.

This will get me the PropertyInfo of the Value property. However, I would also like to get information about the parent property, that is the InnerModel property.

到目前为止,我所知道的是我可以执行以下操作:

What I know so far is that I can do the following:

((MemberExpression)func.Body).Expression

获取父属性的信息.但是,似乎无法从Expression本身提取PropertyInfo.

to fetch information of the parent property. However, it doesn't seem to be possible to extract a PropertyInfo from the Expression itself.

真的有某种方法可以获取表达式的PropertyInfo吗?

Is there some way of actually retrieving the PropertyInfo of the Expression?

需要澄清的是,尝试这样做可能是一种不好的方法,但是可以这样做: 我不能为此使用EntityFramework,只是为了确保它被理解.

To clarify, and it might be a bad way of attempting it but here goes: I can't use EntityFramework for this, just to make sure that that is understood.

我需要通过API与之通信的数据库.

There is a database which I need to communicate with through an API.

该数据库获得了通常的联系方式,例如: 表线程 UserID-> Users.UserID

This database got the usual relations like the manner: Table Thread UserID -> Users.UserID

现在将其提取到模型中.要遵循上面的示例:

This now extract to models. To follow the above example:

class Thread {
    [Reference(USER_USERID)]
    [TableKey(THREAD_USERID)]
    public User user {
        get;set;
    }
}

class User {
     [TableKey(USER_USERID)]
     public int UserId {
         get;set;
     }
}

现在,我想对此进行查询.因此,我想:嘿,让我们使用表达式为最终用户简化如何问问题的方法,是的."

Now I would like to make queries to this. So I thought "Hey, let's use expressions to simplify for the end user on how to ask for stuff, yay."

因此,我们可以做类似EqualTo(o => o.user.UserId,1);

So, we could do something like EqualTo(o => o.user.UserId, 1);

但是,由于TableKey属性与引用键不同,因此我需要首先从数据库中获取线程表中的userId,然后使用该ID开始向User表询问具有该ID的信息.

However, since the TableKey property differs from the reference key I need to first fetch from the database the userId from the Thread table and then with that Id start asking the User table for information with that id.

也许这澄清了所有这些目的,或者也许没有.

Maybe this clarifies the purpose of all this, or maybe it don't.

推荐答案

您已经确定,表达式的主体是MemberExpression.我们需要查看MemberExpression的两个属性.

As you have already determined, the body of the expression is a MemberExpression. There are two properties of a MemberExpression that we need to look at.

第一个是Member属性.此MemberInfo被调用.在您的示例中,这是Value属性.我们需要查看的第二个属性是Expression属性.这就是成员表达式被调用的内容.在您的示例中,这是{o.InnerModel}.

The first is the Member property. This the MemberInfo being called. In your example this the Value property. The second property that we need to look at is the Expression property. This is what the member expression is being called on. In your example, this is {o.InnerModel}.

{o.InnerModel}是另一个MemberExpression.成员是InnerModel,表达式是o.

{o.InnerModel} is another MemberExpression. The Member is InnerModel, and the Expression is o.

这里有一些代码来获取MemberInfos链

Here is some code to get the chain of MemberInfos

public static void Foo<T, U>(Expression<Func<T, U>> func)
{
    var memberExp = func.Body as MemberExpression;
    while (memberExp != null)
    {
        var memberInfo = memberExp.Member;
        Console.WriteLine(memberInfo.Name);
        memberExp = memberExp.Expression as MemberExpression;
    }
}

当这样称呼时:

Foo<Model, String>(o => o.InnerModel.Value);

它将输出:

  • InnerModel

此:

Foo<Assembly, int>(a => a.EntryPoint.DeclaringType.AssemblyQualifiedName.Length);

将输出:

  • 长度
  • AssemblyQualifiedName
  • DeclaringType
  • EntryPoint

这篇关于从表达式函数获取父属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆