为什么有些对象属性是 UnaryExpression,而另一些对象属性是 MemberExpression?

发布于 2024-09-15 19:40:49 字数 1782 浏览 9 评论 0 原文

根据我的 的答案采取行动使用 lambda 而不是字符串属性名称选择模型属性 问题,想要将属性添加到集合中,如下所示:

var props = new ExportPropertyInfoCollection<JobCard>();
props.Include(model => model.BusinessInstallNumber).Title("Install No").Width(64).KeepZeroPadding(true);
props.Include(model => model.DeviceName).Title("Device").Width(70);
props.Include(model => model.DateRequested).Title("Request Date").Format("{0:dd/MM/yyyy}").Width(83);

我在 Include 方法中编写了以下代码:

public class PropertyCollection<T>
{
    public void Include(Expression<Func<T, object>> expression)
    {
        var memberExpression = expression.Body as MemberExpression;
        if (memberExpression != null)
        {
            var pes = new ExportPropertyInfoBuilder {Property = new ExportPropertyInfo {Property = memberExpression.Member as PropertyInfo}};
            Properties.Add(pes.Property.Property.Name, pes.Property);
            return pes;
    }

但是,在运行时在代码中,我发现一些 lambda 产生了预期的 MemberExpression 值,但其他 lambda 产生了 UnaryExpression 值。在使用 lambda 添加所有属性之前,我必须将第一行代码更改为以下内容:

var memberExpression = expression.Body as MemberExpression ?? ((UnaryExpression) expression.Body).Operand as MemberExpression;

POCO 业务对象中的所有属性都是“简单”类型,即 string、DateTime、int、bool 等。它们用几个不同的 DataAnnotations 属性进行装饰。

是什么导致我的示例中的某些 lambda 产生 MemberExpression 值和其他 UnaryExpression 值?在我的示例中,第一个 UnaryExpression 位于第三行,即 DateTime 属性,但布尔属性也会生成 UnaryExpressions

Acting on the answer to my Select a model property using a lambda and not a string property name question, wanting to add properties to a collection as follows:

var props = new ExportPropertyInfoCollection<JobCard>();
props.Include(model => model.BusinessInstallNumber).Title("Install No").Width(64).KeepZeroPadding(true);
props.Include(model => model.DeviceName).Title("Device").Width(70);
props.Include(model => model.DateRequested).Title("Request Date").Format("{0:dd/MM/yyyy}").Width(83);

I wrote the following code in the Include method:

public class PropertyCollection<T>
{
    public void Include(Expression<Func<T, object>> expression)
    {
        var memberExpression = expression.Body as MemberExpression;
        if (memberExpression != null)
        {
            var pes = new ExportPropertyInfoBuilder {Property = new ExportPropertyInfo {Property = memberExpression.Member as PropertyInfo}};
            Properties.Add(pes.Property.Property.Name, pes.Property);
            return pes;
    }

However, on running the code, I found some of the lambdas yielded MemberExpression values as expected, but others yielded UnaryExpression values. I had to change the first line of code to the following before I could add all my properties using lambdas:

var memberExpression = expression.Body as MemberExpression ?? ((UnaryExpression) expression.Body).Operand as MemberExpression;

All the properties are 'simple' types, i.e. string, DateTime, int, bool, etc. in a POCO business object. They are decorated with several varying DataAnnotations attributes.

What causes some of the lambdas in my example to yield MemberExpression values and others UnaryExpression values? In my example, the first UnaryExpression is on the third line, the DateTime property, but boolean properties also result in UnaryExpressions.

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

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

发布评论

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

评论(1

从来不烧饼 2024-09-22 19:40:49

我想我知道问题是什么。您的表达式返回类型object

如果将其更改为 Expression> ,则应正确推断返回类型,并且 UnaryExpression (我假设是某种装箱操作)应该不会发生。

更新:

Include 的签名应为:

public void Include<T, R>(Expression<Func<T, R>> expression)

I think I know what the problem is. Your expression returns type object.

If you change this to Expression<Func<T, R>> the return type should be correctly inferred, and UnaryExpression (which I will assume is some boxing operation) should not occur.

Update:

The signature for Include should be:

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