按键名称的IDictionary 上的Expression.ArrayAccess()

时间:2021-10-05 20:29:08

How do I create expression tree for reading a value by array access for an IDictionary<string,object>?

如何通过数组访问为IDictionary 创建表达式树来读取值? ,object>

I want to represent:

我想代表:

((IDictionary<string,object>)T)["KeyName"]

I can't find any example of ArrayAccess being used to access with string name. I want something like:

我找不到任何用于访问字符串名称的ArrayAccess示例。我想要的东西:

var parameterExpression = Expression.Parameter(typeof(IDictionary<string, object>));

var paramIndex = Expression.Constant("KeyName");

var arrayAccess = Expression.ArrayAccess(parameterExpression, paramIndex);

I get error stating it is not an array. What is correct way to do this?

我收到错误声明它不是一个数组。这样做的正确方法是什么?

1 个解决方案

#1


You probably* want to access the property Item:

您可能*想要访问属性Item:

var dic = new Dictionary<string, object> {
    {"KeyName", 1}
};
var parameterExpression = Expression.Parameter(typeof (IDictionary<string, object>), "d");
var constant = Expression.Constant("KeyName");
var propertyGetter = Expression.Property(parameterExpression, "Item", constant);

var expr = Expression.Lambda<Func<IDictionary<string, object>, object>>(propertyGetter, parameterExpression).Compile();

Console.WriteLine(expr(dic));

If you pop the hood on a class with an indexer, there is an Item property. Consider this example:

如果您在带有索引器的类上弹出引擎盖,则会有一个Item属性。考虑这个例子:

class HasIndexer {
    public object this[int index] {
        get { return null; }
    }
}

has the following (relevant to indexers) IL:

具有以下(与索引器相关)IL:

.property instance object Item(
    int32 index
)
{
    .get instance object ConsoleApplication8.HasIndexer::get_Item(int32)
}

.method public hidebysig specialname 
    instance object get_Item (
        int32 index
    ) cil managed 
{
    // Method begins at RVA 0x2050
    // Code size 2 (0x2)
    .maxstack 8

    IL_0000: ldnull
    IL_0001: ret
} // end of method HasIndexer::get_Item

#1


You probably* want to access the property Item:

您可能*想要访问属性Item:

var dic = new Dictionary<string, object> {
    {"KeyName", 1}
};
var parameterExpression = Expression.Parameter(typeof (IDictionary<string, object>), "d");
var constant = Expression.Constant("KeyName");
var propertyGetter = Expression.Property(parameterExpression, "Item", constant);

var expr = Expression.Lambda<Func<IDictionary<string, object>, object>>(propertyGetter, parameterExpression).Compile();

Console.WriteLine(expr(dic));

If you pop the hood on a class with an indexer, there is an Item property. Consider this example:

如果您在带有索引器的类上弹出引擎盖,则会有一个Item属性。考虑这个例子:

class HasIndexer {
    public object this[int index] {
        get { return null; }
    }
}

has the following (relevant to indexers) IL:

具有以下(与索引器相关)IL:

.property instance object Item(
    int32 index
)
{
    .get instance object ConsoleApplication8.HasIndexer::get_Item(int32)
}

.method public hidebysig specialname 
    instance object get_Item (
        int32 index
    ) cil managed 
{
    // Method begins at RVA 0x2050
    // Code size 2 (0x2)
    .maxstack 8

    IL_0000: ldnull
    IL_0001: ret
} // end of method HasIndexer::get_Item