I am new to Roslyn and I am trying to parse a cs file and get out the functions and properties.
API Examples
//won't work any more
var tree = SyntaxTree.ParseText(...);
var tree = SyntaxTree.ParseFle(...);
var root = tree.Root;
API seems to have changed and it won't work any more. Guess it works with 2012 Roslyn CTP only by the look of it.
I then try the newer example:
//won't work neither
var compilation = CSharpCompilation.Create("HelloWorld")
.AddReferences( new MetadataFileReference(
typeof(object).Assembly.Location)) .AddSyntaxTrees(tree);
var tree = CSharpSyntaxTree.ParseText(strSourceCode);
var root = tree.GetRoot();
Again, it won't work any more because MetadataFileReference
is an abstract class in the version of Roslyn I got. API changed again? I am not sure.
VS & Packages
I am using VS2013 PRO UPDATE 2 from my company pc. 2015 July 05 to date. No, I don't have a computer with VS2015, I'm not sure if that matters.
I open a new WinForm project, then in NuGet PM I installed Roslyn as instructed by https://github.com/dotnet/roslyn. Here is my install log:
PM> Install-Package Microsoft.CodeAnalysis -Pre
Attempting to resolve dependency 'Microsoft.CodeAnalysis.CSharp.Workspaces (= 1.0.0-rc3)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.CSharp (= 1.0.0-rc3)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.Common (= 1.0.0-rc3)'.
Attempting to resolve dependency 'System.Collections.Immutable (≥ 1.1.36)'.
Attempting to resolve dependency 'System.Reflection.Metadata (≥ 1.0.21)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.Analyzers (= 1.0.0-rc3)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.Workspaces.Common (= 1.0.0-rc3)'.
Attempting to resolve dependency 'Microsoft.Composition (≥ 1.0.27)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.VisualBasic.Workspaces (= 1.0.0-rc3)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.VisualBasic (= 1.0.0-rc3)'.
Installing 'System.Collections.Immutable 1.1.36'.
You are downloading System.Collections.Immutable from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=329770. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'System.Collections.Immutable 1.1.36'.
Installing 'System.Reflection.Metadata 1.0.21'.
You are downloading System.Reflection.Metadata from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=329770. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'System.Reflection.Metadata 1.0.21'.
Installing 'Microsoft.CodeAnalysis.Analyzers 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.Analyzers from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.Analyzers 1.0.0-rc3'.
Installing 'Microsoft.CodeAnalysis.Common 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.Common from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.Common 1.0.0-rc3'.
Installing 'Microsoft.CodeAnalysis.CSharp 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.CSharp from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.CSharp 1.0.0-rc3'.
Installing 'Microsoft.Composition 1.0.27'.
You are downloading Microsoft.Composition from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=329770. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.Composition 1.0.27'.
Installing 'Microsoft.CodeAnalysis.Workspaces.Common 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.Workspaces.Common from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.Workspaces.Common 1.0.0-rc3'.
Installing 'Microsoft.CodeAnalysis.CSharp.Workspaces 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.CSharp.Workspaces from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.CSharp.Workspaces 1.0.0-rc3'.
Installing 'Microsoft.CodeAnalysis.VisualBasic 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.VisualBasic from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.VisualBasic 1.0.0-rc3'.
Installing 'Microsoft.CodeAnalysis.VisualBasic.Workspaces 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.VisualBasic.Workspaces from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.VisualBasic.Workspaces 1.0.0-rc3'.
Installing 'Microsoft.CodeAnalysis 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis 1.0.0-rc3'.
Adding 'System.Collections.Immutable 1.1.36' to MyWinForm.
Successfully added 'System.Collections.Immutable 1.1.36' to MyWinForm.
Adding 'System.Reflection.Metadata 1.0.21' to MyWinForm.
Successfully added 'System.Reflection.Metadata 1.0.21' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.Analyzers 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.Analyzers 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.Common 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.Common 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.CSharp 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.CSharp 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.Composition 1.0.27' to MyWinForm.
Successfully added 'Microsoft.Composition 1.0.27' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.Workspaces.Common 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.Workspaces.Common 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.CSharp.Workspaces 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.CSharp.Workspaces 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.VisualBasic 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.VisualBasic 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.VisualBasic.Workspaces 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.VisualBasic.Workspaces 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.CodeAnalysis 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis 1.0.0-rc3' to MyWinForm.
My Target
I simply want to:
Can I have some working examples?
A few things here:
I strongly recommend you use Visual Studio 2015 for Roslyn related tasks. Some parts of Roslyn depend on VS 2015 and the newest MSBuild tools. Visual Studio 2015 Community is free too!
Note: I'd also recommend using the rc-2 bits from NuGet, not the rc-3 bits. You've entered the Roslyn world at a strange time (in-between releases) and this should make your life easier until VS2015 ships on July 20th.
Use the following to install from NuGet:
Install-Package Microsoft.CodeAnalysis -Version 1.0.0-rc2 -Pre
Use CreateFromAssembly
in the following:
var tree = CSharpSyntaxTree.ParseText(@"
class TestClass
{
void TestMethod ()
{
int i;
}
}");
var Mscorlib = MetadataReference.CreateFromAssembly(typeof(object).Assembly);
var compilation = CSharpCompilation.Create("MyCompilation",
syntaxTrees: new[] { tree }, references: new[] { Mscorlib });
var model = compilation.GetSemanticModel(tree);
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