roslyn

How to compile a C# file with Roslyn programmatically?

白昼怎懂夜的黑 提交于 2019-11-27 18:32:43
I read that you can't compile C# 6.0 with CSharpCodeProvider and therefor trying to do with with Roslyn. But I can't find a good example how to load a file and then compile it to a dll. How should I write something similar to this code with Roslyn? Or is there some other way to do it? Now when I try to compile files that contain reference to projects with C# 6.0 code it just say "The type or namespace name 'x' does not exist in the namespace 'y' (are you missing an assembly reference?)" public string CompileCode() { var provider = new CSharpCodeProvider(); var outputPath = Path.Combine(Path

How to use roslyn c# compiler with visual studio 2015?

泄露秘密 提交于 2019-11-27 17:43:33
I've a bit of confusion about roslyn. What I have done: I've installed vs 2015 community edition and download in extensibilty > download compiler platform sdk. So I created a simple console application: hello world example. Well now I'm expect to choise the c# compiler between the vs2015 default one and roslyn..., but I've not found such option. So my first question is: how to select version of c# compiler? Second I've downloaded master-roslyn and I build, then I found csc.exe, well the odd things is that if I lauch the exe I get c# compiler version 42.42.42.42. ???? Right? Then I've follow

Dynamic invoke of a method using named parameters

僤鯓⒐⒋嵵緔 提交于 2019-11-27 16:58:52
问题 We're currently using .NET 3.5 and part of our application uses dynamic invocation (using MethodBase.Invoke) I am wondering if it is possible to mix in Named Parameters (in .NET 4) with dynamic invocation, to perform something similar to: // Dictionary that holds parameter name --> object mapping var parameters = new Dictionary<string, object>(); // Add parameters .... // Invoke where each parameter will match the one from the method signature. methodInfo.Invoke(obj, parameters); Is there any

Adding Auto-Implemented Property to class using Roslyn

送分小仙女□ 提交于 2019-11-27 16:19:26
问题 I'm trying to learn Roslyn by building an existing but simple application from the ground up, which seems to be a productive way to learn this. Anyhow, I have the following code: var root = (CompilationUnitSyntax)document.GetSyntaxRoot(); // Add the namespace var namespaceAnnotation = new SyntaxAnnotation(); root = root.WithMembers( Syntax.NamespaceDeclaration( Syntax.ParseName("ACO")) .NormalizeWhitespace() .WithAdditionalAnnotations(namespaceAnnotation)); document = document

Bitwise-or operator used on a sign-extended operand in Visual Studio 2015

柔情痞子 提交于 2019-11-27 16:06:10
问题 I just tried installing Visual Studio 2015, and when trying to compile an old project, I got the warning CS0675 Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first for a piece of code that does not give the same warning when compiling in Visual Studio 2013. I found out that all it takes to reproduce is this very simple code: short a = 0; int b = 0; a |= (short)b; Now, I have read this SO question, I have read Eric Lippert's blog post on this

MSBuildWorkspace.Create() throws exception

妖精的绣舞 提交于 2019-11-27 16:06:01
问题 I have Visual Studio 2013. I also have installed MSBuild Tools 2013. The following code gives me exception var workspace=MSBuildWorkspace.Create(); Here is the exception Could not load file or assembly 'Microsoft.Build, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. What am I doing wrong ? 回答1: You need to install the BuildTools for Visual Studio 2015. 回答2: You could compile Roslyn against an older

How to get a Roslyn FieldSymbol from a FieldDeclarationSyntax node?

邮差的信 提交于 2019-11-27 15:01:17
I'm trying to use Roslyn to determine the publically-exposed API of a project (and then do some further processing using this information, so I can't just use reflection). I'm using a SyntaxWalker to visit declaration syntax nodes, and calling IModel.GetDeclaredSymbol for each. This seems to work well for Methods, Properties, and Types, but it doesn't seem to work on fields. My question is, how do I get the FieldSymbol for a FieldDeclarationSyntax node? Here's the code I'm working with: public override void VisitFieldDeclaration(FieldDeclarationSyntax node) { var model = this._compilation

Null-conditional operator and string interpolation in C# 6

独自空忆成欢 提交于 2019-11-27 14:38:42
问题 Do the null-conditional operator and interpolated strings syntax resolve to just syntactic sugar? The null-conditional operator ( ?. ), which allows code clean-up through reducing "excessive" null checking, and interpolated strings ( ("\{X}, \{Y}") ), which brings the arguments and format into one, are new features in C# 6. Do these get compiled to their undesirable counterparts (i.e. the ugly code we sought to avoid)? I apologize for the naïve question, I don't have the best understanding of

Using C# 6 features with CodeDomProvider (rosyln)

做~自己de王妃 提交于 2019-11-27 13:30:18
CodeDomProvider objCodeCompiler = CodeDomProvider.CreateProvider( "CSharp" ); CompilerParameters objCompilerParameters = new CompilerParameters(); ... CompilerResults objCompileResults = objCodeCompiler.CompileAssemblyFromFile( objCompilerParameters, files.ToArray() ); When I compile my files I get: FileFunctions.cs(347): Error: Unexpected character '$' Does anyone know how to get string interpolation working with CodeDom compiling? I found this link: How to target .net 4.5 with CSharpCodeProvider? So I tried: var providerOptions = new Dictionary<string, string>(); providerOptions.Add(

How to read XML documentation comments using Roslyn

a 夏天 提交于 2019-11-27 12:13:40
问题 I would like to be able to read XML documentation comments while parsing C# source code using Roslyn. /// <summary> /// Documentation... /// </summary> I tried setting the ParseDocumentationComments in the ParseOptions, but it doesn't seem to have an effect? var parseOptions = ParseOptions.Default.WithParseDocumentationComments(true); SyntaxTree unit = SyntaxTree.ParseFile(file, parseOptions); 回答1: You'll need to either: Look at the LeadingTrivia of the syntax that contains the XML doc