ScopeCompile 期望变量已经存在,这不是否定了整点吗?

ScopeCompile expects variables to already exist, doesn't that negate the whole point?

我正在使用 CSharpEval's ScopeCompile 功能来允许我在不同的数据上重新使用编译的表达式:

        var exp = new CompiledExpression<int>("a.Count");
        var func = exp.ScopeCompile();

但是我在 ScopeCompile 调用中遇到了这个异常:

An exception of type 'ExpressionEvaluator.Parser.ExpressionParseException' occurred in ExpressionEvaluator.dll but was not handled in user code

Additional information: Cannot resolve symbol "a" at line 1 char 0

我不明白发生了什么。我的 link 中的示例允许缓存范围编译的表达式,如果在编译表达式时我的表达式中的变量已经存在,它有什么用?!

文档非常有限,我对 C# 的这一领域了解不够,无法轻松阅读源代码。

你可以尝试关注,

public class ScopeContext<TArg,TRet>{

    public dynamic Scope {get;set;}

    public Func<TArg,TRet> Expression;

    public ScopeContext(string exp){
        TypeRegistry tr = new TypeRegistry();
        tr.RegisterSymbole("scope",Scope);

        Expression = (new CompiledExpression<TRet>(exp)
        {
            TypeRegistry = tr
        }).ScopeCompile<TArg>();
    }

}

// usage

ScopeContext<object,object> f = 
     new ScopeContext<object,object>("scope.Count");


// you can now change scope dynamically...
f.Scope = new List<int>();

var result = f.Expression(null);