.net 获取类型';使用文字表达式lambda语法将索引器作为PropertyInfo

.net 获取类型';使用文字表达式lambda语法将索引器作为PropertyInfo,.net,expression,expression-trees,.net,Expression,Expression Trees,我寻求一种使用lambda语法获取类型索引器属性的方法。这个问题向您展示了如何使用反射来获取它: 我测试了这个解决方案,效果不错,但对我的口味来说,它有点太大了。我想这样做: Expression<Func<Dictionary<string, string>, string>> index = e => e[default(string)]; 表达式索引= e=>e[默认值(字符串)]; 这为索引器的get访问器提供了一个MethodIn

我寻求一种使用lambda语法获取类型索引器属性的方法。这个问题向您展示了如何使用反射来获取它:

我测试了这个解决方案,效果不错,但对我的口味来说,它有点太大了。我想这样做:

Expression<Func<Dictionary<string, string>, string>> index =
    e => e[default(string)];
表达式索引=
e=>e[默认值(字符串)];
这为索引器的get访问器提供了一个
MethodInfo
,但我需要将索引器作为
PropertyInfo
获取,以便将其传递到
表达式中。MakeIndex

var p_formcollection = Expression.Parameter(typeof(FormCollection), nameof(FormCollection));
//helper method that does the same thing as in my OP
//I also used Dictionary<,> in my OP, but it's really a FormCollection
var indexer = ExpressionHelpers.GetMethodInfo<FormCollection, string>(e => e[default(string)]);
var collectionAccess = Expression.Call(p_formcollection, indexer, Expression.Constant(pi.PropertyInfo.Name));

我该怎么办?

本来打算删除这个,但我想保留它,以防有用

我真傻,竟然不想用
表达式。调用
而不是
表达式。MakeIndex

var p_formcollection = Expression.Parameter(typeof(FormCollection), nameof(FormCollection));
//helper method that does the same thing as in my OP
//I also used Dictionary<,> in my OP, but it's really a FormCollection
var indexer = ExpressionHelpers.GetMethodInfo<FormCollection, string>(e => e[default(string)]);
var collectionAccess = Expression.Call(p_formcollection, indexer, Expression.Constant(pi.PropertyInfo.Name));
var p_formcollection=Expression.Parameter(typeof(formcollection)、nameof(formcollection));
//helper方法,它执行与我的OP中相同的操作
//我在我的作品中也使用了字典,但它实际上是一个FormCollection
var indexer=ExpressionHelpers.GetMethodInfo(e=>e[default(string)]);
var collectionAccess=Expression.Call(p_formcollection,indexer,Expression.Constant(pi.PropertyInfo.Name));