0
我想建立一个系统,加载功能代表字典,然后他们可以从任何地方调用环境要求字典中的代表。Expression.Lambda变量''的类型'System.String'引用范围'',但它没有被定义
我的函数是一个格式Func<string, string>
的。
我的代码是
var methods = typeof(Keywords)
.GetMethods()
.Where(mt => mt.GetCustomAttributes(typeof(KDTAttribute), false).Count() > 0);
foreach (var method in methods)
{
string key = ((KDTAttribute)method.GetCustomAttributes(typeof(KDTAttribute), false)[0]).Keyword;
var combinedArgumentsExp = new Expression[] { Expression.Parameter(typeof(string),"param") };
var mtCall = Expression.Call(Expression.Constant(me), method,combinedArgumentsExp);
ParameterExpression targetExpr = Expression.Parameter(typeof(string), "param");
Func<string, string> result = Expression.Lambda<Func<string, string>>(mtCall, targetExpr).Compile();
retVal.Add(key, result);
}
我上Expression.Lambda
线除外:
变量从范围 '' 'System.String' 引用类型的 'PARAM',但它不是定义。
P.S:
如果有更好的方式来代表加载在运行时的字典,我会很乐意的任何建议。
谢谢,作品像一个魅力。 – Amorphis