从表达式树索引访问 [英] Accessing indexer from expression tree

查看:128
本文介绍了从表达式树索引访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的过滤功能。该过滤器将是一个表达式树的构建由用户。将有大约30个字段,用户可以使用用于过滤。我认为最好的方法是创建索引和枚举类型的索引来访问所需的值对象模型

I am working on a filtering function. The filter will be an expression tree build by an user. There will be about 30 fields the user can use for filtering. I think the best way is to create the object model with indexer and to access required values by index of enum type.

请参阅下面的例子:

enum Field
{
    Name,
    Date,
}

class ObjectModel
{
    object this[Field Key]
    {
        get 
        {
            //...
            return xx;
        }
    }
}



我想问一下怎么样我可以从一个表达式树访问索引器。

I would like to ask how can I access an indexer from an expression tree.

推荐答案

该索引是一个简单的属性,通常称为项目。这意味着,你可以访问索引器一样使用其名称的任何其他属性。

The indexer is a simple property, normally called Item. This means, you can access the indexer like any other property by using its name.

索引属性的名称可以由类的实现者借助于改变 IndexerName 属性

The name of the indexer property can be changed by the implementor of the class by means of the IndexerName attribute.

要可靠地获得索引器属性的实际名称,您不得不反思类和获得的 DefaultMember 属性。结果
的更多信息,可以发现<一个HREF =htt​​p://social.msdn.microsoft.com/Forums/en-US/vstscode/thread/60de101a-278d-4674-bc1a-0a04210d566c>这里。

To reliably get the actual name of the indexer property, you have to reflect on the class and obtain the DefaultMember attribute.
More information can be found here.

这篇关于从表达式树索引访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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