如何使用 Linq 表达式访问字典项
我想使用 Linq 表达式构建一个 Lambda 表达式,该表达式能够使用字符串索引访问“属性包”样式字典中的项目。我正在使用.Net 4。
static void TestDictionaryAccess()
{
ParameterExpression valueBag = Expression.Parameter(typeof(Dictionary<string, object>), "valueBag");
ParameterExpression key = Expression.Parameter(typeof(string), "key");
ParameterExpression result = Expression.Parameter(typeof(object), "result");
BlockExpression block = Expression.Block(
new[] { result }, //make the result a variable in scope for the block
Expression.Assign(result, key), //How do I assign the Dictionary item to the result ??????
result //last value Expression becomes the return of the block
);
// Lambda Expression taking a Dictionary and a String as parameters and returning an object
Func<Dictionary<string, object>, string, object> myCompiledRule = (Func<Dictionary<string, object>, string, object>)Expression.Lambda(block, valueBag, key).Compile();
//-------------- invoke the Lambda Expression ----------------
Dictionary<string, object> testBag = new Dictionary<string, object>();
testBag.Add("one", 42); //Add one item to the Dictionary
Console.WriteLine(myCompiledRule.DynamicInvoke(testBag, "one")); // I want this to print 42
}
在上面的测试方法中,我想将字典项值(即 testBag["one"] 分配到结果中)。请注意,我已将传入的 Key 字符串分配到结果中以演示分配调用。
I want to build a Lambda Expression using Linq Expressions that is able to access an item in a 'property bag' style Dictionary using a String index. I am using .Net 4.
static void TestDictionaryAccess()
{
ParameterExpression valueBag = Expression.Parameter(typeof(Dictionary<string, object>), "valueBag");
ParameterExpression key = Expression.Parameter(typeof(string), "key");
ParameterExpression result = Expression.Parameter(typeof(object), "result");
BlockExpression block = Expression.Block(
new[] { result }, //make the result a variable in scope for the block
Expression.Assign(result, key), //How do I assign the Dictionary item to the result ??????
result //last value Expression becomes the return of the block
);
// Lambda Expression taking a Dictionary and a String as parameters and returning an object
Func<Dictionary<string, object>, string, object> myCompiledRule = (Func<Dictionary<string, object>, string, object>)Expression.Lambda(block, valueBag, key).Compile();
//-------------- invoke the Lambda Expression ----------------
Dictionary<string, object> testBag = new Dictionary<string, object>();
testBag.Add("one", 42); //Add one item to the Dictionary
Console.WriteLine(myCompiledRule.DynamicInvoke(testBag, "one")); // I want this to print 42
}
In the above test method, I want to assign the Dictionary item value i.e. testBag["one"] into the result. Note that I have assigned the passed in Key string into the result to demonstrate the Assign call.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用以下代码来访问
Dictionary
的 Item 属性,这是应该实现此目的的代码更改。
You can use the following to access the Item property of the
Dictionary
Here is the code change that should do the trick.