问题
I'm creating a script editor for my application, and I'd like to have it be able to run both C# and VB.NET scripts using Roslyn. I got this working with C# by using the CSharpScript
class in the Microsoft.CodeAnalysis.CSharp.Scripting
assembly/namespace, however there is no equivalent VisualBasicScript
class that I can find, nor a Microsoft.CodeAnalysis.VisualBasic.Scripting
assembly. I've scoured the web and can find nothing useful about running VB.NET scripts, and all the samples on the Roslyn github site are C#-specific. Am I missing something, or does support for running VB.NET scripts simply not exist the way it does for C# scripts?
回答1:
Visual Basic Scripting is not currently available, presumably because it's not finished.
But its source is is the Roslyn repo, so you could try building it yourself.
When I do that, code like the following works for me:
Dim result = VisualBasicScript.RunAsync("Dim result = 1+1").Result
For Each variable In result.Variables
Console.WriteLine($"{variable.Name}: {variable.Value}")
Next
But this code does not work for me (it fails when compiling the script):
Console.WriteLine(VisualBasicScript.EvaluateAsync("1+1").Result)
I'm not sure whether this is because it's not finished, or whether it's intentional difference from C# scripting.
来源:https://stackoverflow.com/questions/37670024/can-roslyn-be-used-for-vb-net-scripting