我想在运行时分配一个MaterialDesign图标,以允许根据用户配置配置按钮。
即
cfgvalue="PackIconKind.Ambulance";myicon.Kind = eval(cfgvalue);
我认为这可以使用Roslyn/CSharpScript包来实现,如下所示:
PackIconKind result = 0;
CSharpScript.EvaluateAsync<PackIconKind>(cfgvalue,
ScriptOptions.Default.WithReferences("MaterialDesignThemes.Wpf")
.WithImports("MaterialDesignThemes.Wpf"))
.ContinueWith(s => result = s.Result).Wait();
myicon.Kind = result;不幸的是,EvaluateAsync行抛出了错误:
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我使用的是.Net 4.6.1和Microsoft.CodeAnalysis.CSharp 2.4.0。我使用了这个页面作为参考:https://github.com/dotnet/roslyn/wiki/Scripting-API-Samples
还有一些其他的用法示例,但他们谈到了使用AddReference和1.1.1版的CSharpScript库,因此可能不再相关。
使用这种技术我想要的东西是可以实现的吗?
提前谢谢。史蒂夫
发布于 2018-02-24 03:14:03
我得到了同样的错误。我更改了很多东西,但这是我的最终代码。请注意,我在脚本中使用了"System.Windows.Point“。我的代码是过度杀伤力,但它应该使它适用于任何应该在您的代码上下文中运行的脚本(唯一要做的就是在脚本中使用正确的用法)。
别忘了做"scriptOptions = scrip...“并使用"ScriptOptions“调用Evaluate。希望它能帮助你解决问题。
我的代码:
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);我的脚本:
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;https://stackoverflow.com/questions/47433776
复制相似问题