I want to assign a MaterialDesign icon at runtime to allow buttons to be configured based on user configuration.
i.e.
cfgvalue="PackIconKind.Ambulance"; myicon.Kind = eval(cfgvalue);
I thought this could be achieved using the Roslyn/CSharpScript package like this:
PackIconKind result = 0;
CSharpScript.EvaluateAsync<PackIconKind>(cfgvalue,
ScriptOptions.Default.WithReferences("MaterialDesignThemes.Wpf")
.WithImports("MaterialDesignThemes.Wpf"))
.ContinueWith(s => result = s.Result).Wait();
myicon.Kind = result;
Unfortunately the EvaluateAsync line throws errors:
Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Resources.ResourceManager\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Resources.ResourceManager.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Exception thrown: 'Microsoft.CodeAnalysis.Scripting.CompilationErrorException' in Microsoft.CodeAnalysis.Scripting.dll
error CS0400: The type or namespace name 'MaterialDesignThemes.Wpf.PackIconKind, MaterialDesignThemes.Wpf, Version=2.3.0.823, Culture=neutral, PublicKeyToken=null' could not be found in the global namespace (are you missing an assembly reference?)
error CS0246: The type or namespace name 'MaterialDesignThemes' could not be found (are you missing a using directive or an assembly reference?)
(1,1): error CS0103: The name 'PackIconKind' does not exist in the current context
I'm using .Net 4.6.1 and Microsoft.CodeAnalysis.CSharp 2.4.0. I used this page for reference: https://github.com/dotnet/roslyn/wiki/Scripting-API-Samples
There are a few other examples of usage out there but they talk about using AddReference and version 1.1.1 of the CSharpScript library so are maybe no longer relevant.
Is what I want achievable using this technique?
Thanks in advance. Steve
I got the same error. I changed many things but this is my final code. Please note that I was using "System.Windows.Point" in my script. My code is overkill but it should make it works for any script that should run in your code context (the only thing will be to use proper using in script).
Don't forget to do "scriptOptions = scrip..." and call Evaluate with the "ScriptOptions". Hope it could help solve your problem.
My Code:
var scriptOptions = ScriptOptions.Default;
var asms = AppDomain.CurrentDomain.GetAssemblies(); // .SingleOrDefault(assembly => assembly.GetName().Name == "MyAssembly");
foreach (Assembly asm in asms)
{
scriptOptions = scriptOptions.AddReferences(asm);
}
scriptOptions = scriptOptions.AddImports("System");
scriptOptions = scriptOptions.AddImports("System.Windows");
Point[] points = await CSharpScript.EvaluateAsync<Point[]>(Code, scriptOptions);
My script:
Point[] pts = new Point[100];
for (int n = 0; n < 100; n++)
{
double x = n;
double y = 3.4 * x + 7;
pts[n] = new Point(n, y);
}
return pts;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With