Use static functions in runtime code (rosyln)

Hello, I'm trying to import a static class (using static namespace.class) into rosyln executing code (await CSharpScript.EvaluateAsync(code)). In this example, the function Query is a static function in the running assembly.
// Normal math functions
int value = 15 + 45 * 34;

// Reference an equation in the history panel
int reference = Query("001");

// Return the final solution
return value + reference;
// Normal math functions
int value = 15 + 45 * 34;

// Reference an equation in the history panel
int reference = Query("001");

// Return the final solution
return value + reference;
namespace MathNotationTool.Extensions
{
public static class CodeExt
{
public static decimal Query(string entry)
{
return ViewModel.Calculator.ViewModel.History.Where(x => x.Name == entry).FirstOrDefault()?.Value ?? 0;
}
}
}
namespace MathNotationTool.Extensions
{
public static class CodeExt
{
public static decimal Query(string entry)
{
return ViewModel.Calculator.ViewModel.History.Where(x => x.Name == entry).FirstOrDefault()?.Value ?? 0;
}
}
}
Currently I'm setting the default code options in the startup function:
ScriptOptions.Default.WithReferences(typeof(App).Assembly).WithImports("MathNotationTool.Extensions.CodeExt");
ScriptOptions.Default.WithReferences(typeof(App).Assembly).WithImports("MathNotationTool.Extensions.CodeExt");
But this doesn't seem to work. The function isn't recognized, and importing the namespace in the executing code fails in the same way (namespace/assembly not found). Is there a way to import a static class into the memory-runtime?
1 Reply
Arch Leaders
Arch Leaders2y ago
Okay I found the issue. I just misunderstood how ScriptOptions.Default worked. I thought it set the default when using WithImport, but it just returns the options object with the imports (which makes perfect sense now I think about it).