0

I'm trying to execute a script dynamically, via CSharpScript, following the example here: example

public class Feature
{
    public class Params
    {
        public TypeA ParamA { get; set; }
        public TypeB ParamB { get; set; }
    }
    
    public static TypeA paramA;
    public static TypeB paramB;

    public ExecuteScript(foo, bar)
    {
        paramA = GetParameterA(foo);
        paramB = GetParameterB(paramA, bar);

        string scriptCommand = @"
            function1(paramA)
            ... // do stuff
            return ""done"";
            ";

        var options = ScriptOptions.Default
            .WithImports(
                "System",
                "System.Collections.Generic",
                "System.Linq",
                );


        var items = new Params()
        {
            ParamA = paramA,
            ParamB = paramB
        };
        var result = CSharpScript.EvaluateAsync<string>(scriptCommand, options, globals: items).Result;
    }
}

Nevertheless, when running the script, I'm getting an error:

error CS0103: The name 'paramB' does not exist in the current context

Any idea what I'm doing wrong? I'm guess it's something to do with the nested Class definition but not sure exactly what I'm missing here :/

EDIT:

When using local variables in the scope of the ExecuteScript() method

TypeA paramA;
TypeB paramB;

and then using them in result:

var result = CSharpScript.EvaluateAsync<string>(scriptCommand, options, globals: new { paramA, paramB }).Result;

The error for the var result line is different and is:

error CS0122: '<>f__AnonymousType0<paramA, paramB>.paramB' is >inaccessible due to its protection level

Yafim Simanovsky
  • 531
  • 7
  • 26
  • How about telling us which line this error applies to. Or is your intent to let us scour your source code, hunting for the line that's associated with this error? (That said, why would you use static fields for paramA and paramB? Given your code in the question, there doesn't seem to be no reason for them to be static nor to be fields, but rather local variables...) –  Oct 02 '22 at 16:11
  • @MySkullCaveIsADarkPlace sorry for not specifying. The error refers to the execution of the code in the scriptCommand itself, i.e. in the line `var = result`. I've edited the question to include another attempt with local variables. – Yafim Simanovsky Oct 02 '22 at 20:17
  • Just one more clarification: Do you get the error when building/compiling your program, or only when you are trying to execute it? Your script command string is only having a single paramA parameter, so this could be it, but i am a bit puzzled as the error you got is a compile-time error that would normally occur during building and not when executing of your program. And i don't know if CSharpScript is employing some compile process under the hood that would generate such an error during execution of your program... –  Oct 02 '22 at 20:21
  • Hmm... well, I only get it when executing. No compile errors. I get the error also when both params are used (for example in `//do stuff`, it's just failing at paramB first (in the example I might have switched them but it's the same if I use them like this or switch the order in the scriptCommand. – Yafim Simanovsky Oct 02 '22 at 20:34

0 Answers0