roslyn

Getting type from a symbol in roslyn

喜夏-厌秋 提交于 2019-11-30 04:06:15
问题 What is the best general purpose way to get a System.Type from Microsoft.CodeAnalysis.ISymbol for different types of symbols ? (e.g. class declarations, variable, properties, etc) I want to be able to do various checks on the type e.g. as checking if the type implements any interface or is cast-able to any interface, just like one can check on System.Type. The problem I am having is that most of the concrete classes used to represent the symbol are internal (see http://source.roslyn.io/) and

Is it possible to compile a single C# code file with the .NET Core Roslyn compiler?

☆樱花仙子☆ 提交于 2019-11-30 03:44:46
In old .NET we used to be able to run the csc compiler to compile a single .cs file or several files. With .NET Core we have dotnet build that insists on having a proper project file. Is there a stand-alone command line compiler that would allow to compile source code files without having a project (and listing referenced dependencies on the same command line)? On Linux, when I have the old csc and the new .NET Core installed, I get these timings: [root@li1742-80 test]# time dotnet build Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core Copyright (C) Microsoft Corporation. All

Pass object instance to Roslyn ScriptEngine

人走茶凉 提交于 2019-11-30 03:35:07
问题 I'm looking for a C# scripting engine, that can interpret blocks of C# code, while maintaing a context. For example, if enter to it: var a = 1; , and then a + 3 , it'll output 4 . I'm aware of MS Roslyn , which indeed do that, but it's a sandbox (respect to the program that launched it). So, if I create an instance of ScriptEngine and an instance of MyClass (just an arbirary class of mine) , I have no option to pass a reference of my_class to script_engine . Is it possible to somehow pass

Using Roslyn compiler with Visual Studio 2013

喜你入骨 提交于 2019-11-30 03:11:13
问题 Is there a way to use the Roslyn compiler with Visual Studio 2013 so that I can take advantage of the new C# 6 features ? Note: Using VS 2015 is not an option. 回答1: Yes , you can compile C# 6 code with Visual Studio 2013. You just have to install the Microsoft.Net.Compilers NuGet package and your code will build just fine (however, note that you need to target .NET 4.5+). But , the IDE won't understand it. You'll get squiggles everywhere you try to use a C# 6 feature, because VS2013's

Type or namespace not found “are you missing assembly reference” while all references are correct

跟風遠走 提交于 2019-11-30 01:43:39
问题 I am trying to use MSBuildWorkspace class . I have all assembly references in my project. When I open the reference in object browser, I see the namespace and the class I am trying to use. But in my following using statement, using Microsoft.CodeAnalysis.MSBuild I am getting a The type or namespace name 'MSBuild' does not exist in the namespace 'Microsoft.CodeAnalysis' (are you missing an assembly reference?) But funny that Syntax highlighter recognizes the type name, its the compiler

How do I declare a var variable with Roslyn?

淺唱寂寞╮ 提交于 2019-11-30 01:31:22
问题 I've got the following piece of code, but I can't find how to get the var TypeSyntax . Any ideas? Syntax.LocalDeclarationStatement( declaration: Syntax.VariableDeclaration( type: Syntax.PredefinedType(Syntax.Token(SyntaxKind.VarKeyword)), variables: Syntax.SeparatedList( Syntax.VariableDeclarator( identifier: Syntax.Identifier(name))) ) ) ); this fails with an Argument exception that says: "keyword" 回答1: I'd use: Syntax.LocalDeclarationStatement( declaration: Syntax.VariableDeclaration( type:

C# 7.3 Enum constraint: Why can't I use the nullable enum?

寵の児 提交于 2019-11-30 01:19:08
Now that we have enum constraint, why doesn't compiler allow me to write this code? public static TResult? ToEnum<TResult>(this String value, TResult? defaultValue) where TResult : Enum { return String.IsNullOrEmpty(value) ? defaultValue : (TResult?)Enum.Parse(typeof(TResult), value); } The compiler says: Error CS0453 The type 'TResult' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable' You can, but you have to add another constraint: the struct constraint. public static void DoSomething<T>(T? defaultValue) where T : struct, Enum { }

Using Roslyn to parse/transform/generate code: am I aiming too high, or too low?

情到浓时终转凉″ 提交于 2019-11-30 00:09:33
(What I'm trying to do is work around the Application.Settings/MVVM problem by generating an interface and wrapper class from the vs-generated settings file.) What I'd like to do is: Parse a class declaration from file Generate an interface declaration based on just the (non static) properties of the class Generate a wrapper class which implements this interface, takes an instance of the original class in the constructor, and 'pipes' all the properties through to the instance. Generate another class which implements the interface directly. My question is two-fold: Am I barking up the wrong

Numerous instances of VBCSCompiler.exe

[亡魂溺海] 提交于 2019-11-29 21:13:29
I just recently downloaded and installed Visual Studio Professional 2015 (14.0.23107.0). The first time I opened up our solution (28 projects) and performed a Build -> Rebuild Solution, my development machine came to an absolute crawl. The CPU was maxed out at 100% and the build never completed - even after > 10 minutes. I opened up Windows Task Manager and noticed: > 10 VBCSCompiler.exe tasks running. When combined, these tasks sent the CPU > 90%. Any idea why there are so many of these tasks running? Any way to stop this from happening? This is the closest thing I can find to someone else

scriptcs hosting - Advantages over Roslyn

纵然是瞬间 提交于 2019-11-29 21:00:19
If I want to support scripting in my application, does scriptcs offer any particular advantages over just using the plain vanilla Roslyn script engine? Unfortunately there's not much documentation on hosting scriptcs yet, but I'll try to give you a short summary. Hosting scriptcs in your application provides several features that vanilla Roslyn doesn't: Pluggable Engines While scriptcs comes with the Roslyn and Mono engines by default, you can easily replace it with another engine, i.e. F# , LOLcode or even Brainfuck . Pre-processing scriptcs will process your scripts and extract things like