roslyn

Loading a script from a cs file and accessing host methods, properties etc?

喜夏-厌秋 提交于 2019-12-02 06:28:39
问题 I'm just having a play with Roslyn but unsure on how to do the following. To keep this simple, lets say I have a host program which has a method like so public void DisplayMessage(string message) { MessageBox.Show(message); } Can I then have a script file called MyScript.csx and then somewhere in the script have something like void Main() { Host.DisplayMessage("I am a script"); } Then I have the host load the file and execute it. If this sort of thing can't be done, is there a scripting

Reading the default namespace through Roslyn API

你离开我真会死。 提交于 2019-12-02 06:17:35
问题 Is there a way to read the default namespace setting from the IProject interface or any other Roslyn interface? I know that I can parse the project's file but I think this should be possible using Roslyn API but I cannot find how to do that. Thanks in advance for information. 回答1: Unfortunately, Roslyn does not expose a way to do that at the moment, but I agree that it is something we will probably need eventually. 回答2: The library Microsoft.Build.Evaluation, which is, I believe, the

How to validate a parameter's type in method when using Roslyn

安稳与你 提交于 2019-12-02 05:06:47
I'm doing code analysis with Roslyn in order to validate that even though I have the following signature public void MyMethod(object anObject, MyCustomObject customObject); I only want to receive, as parameters, a string (1st) and a child from MyCustomObject (2nd). I have no power over the signature, it cannot be changed. Here's what I did to evaluate my method (Here's a snippet) public void OnMethodInvocation(SyntaxNodeAnalysisContext context) { var invocation= context.Node as InvocationExpressionSyntax; var symbol = context.SemanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol; if

Roslyn analyser code fix - Disable preview option

人走茶凉 提交于 2019-12-02 04:54:14
问题 How do I disable the preview dialog that shows up after the light bulb in C# project? Problem I have is, the RegisterCodeFixesAsync makes a call to database and increments the id and this is getting done twice (once during the preview and second time when the action is invoked), instead of incrementing just once, id increments twice 回答1: CodeAction has separate ComputePreviewOperationsAsync() and ComputeOperationsAsync() . Having them return different values is what I believe you're looking

Roslyn Check Type of an Attribute

旧街凉风 提交于 2019-12-02 04:06:07
I'm trying to figure out there proper way to compare attribute data in Roslyn. static bool ResolveAttributes(IMethodSymbol methodSymbol) { var attributes = methodSymbol.GetAttributes(); return null == attributes.FirstOrDefault(attr => isIDEMessageAttribute(attr, typeof(MyAttributeType))); } static bool IsIDEMessageAttribute(AttributeData attribute, Type desiredAttributeType) { //How can I check if the attribute is the type of desired? } How can I check if the attribute is the type of desired? Matt Warren AttributeData.AttributeClass gives you the Roslyn symbol for the attribute. But you've got

Reading the default namespace through Roslyn API

北城余情 提交于 2019-12-02 00:52:11
Is there a way to read the default namespace setting from the IProject interface or any other Roslyn interface? I know that I can parse the project's file but I think this should be possible using Roslyn API but I cannot find how to do that. Thanks in advance for information. Unfortunately, Roslyn does not expose a way to do that at the moment, but I agree that it is something we will probably need eventually. The library Microsoft.Build.Evaluation, which is, I believe, the successor of Roslyn does have this feature, but it is not easy to find. I use the code below to obtain the default

Roslyn compiler version used for projects missing Microsoft.Net.Compilers

做~自己de王妃 提交于 2019-12-01 23:51:09
Solution consists of 2 dll libraries, 1 console app and 1 web app. Only the last project, the web one, uses Microsoft.Net.Compilers NuGet package. Package has been updated to version 2.3.0 now, what means C# 7.1 and VB 15.3. source: Roslyn NuGet packages As 3 other projects do not have Microsoft.Net.Compilers NuGet package installed, what Roslyn compiler and C# version do they use? I use VS.Net 2017 15.2 with .NET Framework 4.7, so I would guess it shall C#7.0, but still Microsoft.Net.Compilers in version 2.2 should be installed there, but is not. The Microsoft.Net.Compilers package overrides

Roslyn analyser code fix - Disable preview option

陌路散爱 提交于 2019-12-01 23:33:51
How do I disable the preview dialog that shows up after the light bulb in C# project? Problem I have is, the RegisterCodeFixesAsync makes a call to database and increments the id and this is getting done twice (once during the preview and second time when the action is invoked), instead of incrementing just once, id increments twice CodeAction has separate ComputePreviewOperationsAsync() and ComputeOperationsAsync() . Having them return different values is what I believe you're looking for. But if you use the common approach of calling CodeAction.Create() , both will return the same values.

Create a Func<> with Roslyn

十年热恋 提交于 2019-12-01 18:29:28
Inspired by this and this article, I'm trying to create a dynamic function with Roslyn. However the mentioned sources are outdated or not complete and I'm not able to create a functional sample. My work so far: var code = @"Func<int, int> doStuffToInt = i => { var result = i; for (var y = i; y <= i * 2; y++) { result += y; } return result; };"; var se = new ScriptEngine(); var session = se.CreateSession(); session.AddReference(typeof(Program).Assembly); session.AddReference(typeof(Expression).Assembly); session.ImportNamespace("System"); session.ImportNamespace("System.Linq"); session

How to specify the equivalent of /features:strict (of csc.exe) to msbuild.exe or in the .csproj file?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 17:51:49
Introduction Consider this simple (and bad) C# class: using System; namespace N { static class C { static void M(DateTime d) { if (d == null) Console.WriteLine("Yes"); else Console.WriteLine("No"); } static void L(object o) { if (o is Nullable) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } } Both methods M and L have serious issues. In M , we ask if a value of the non-nullable struct DateTime is equal to null via the lifted == operator (which exists since DateTime overloads operator == ). This is always falls, and the compiler can tell at compile-time, so we have a branch ( "Yes"