C
Join ServerC#
help
Use static functions in runtime code (rosyln)
ALArch Leaders11/5/2022
Hello,
I'm trying to import a static class (
In this example, the function Query is a static function in the running assembly.
Currently I'm setting the default code options in the startup function:
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?
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;
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");
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?
ALArch Leaders11/5/2022
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).