roslyn

How to modify code before compilation?

非 Y 不嫁゛ 提交于 2019-12-02 16:43:18
Using Roslyn, I would like to modify my C# code before the actual compilation. For now, I will just need something like: [MyAnotatedMethod] public void MyMethod() { // method-body } And based on the annotation, I would like to inject some code at the beginning of the method, and at the end of the method. I'm aware of PostSharp, but that's not what I would like. Is this possible to do with Roslyn? And if yes, could you give me an example? Here is a quick and dirty way of doing what you want. It's based on one of the above comments, which points to SebbyLive . It is just a proof of concept, I

visual studio 2015 update 2 RTM and RC debugging context not available

限于喜欢 提交于 2019-12-02 16:13:05
Update 1: After Updating visual studio to version 2015 update 2 RTM the problem still exist. Update 2 patch available 4/12/2016 : Thanks to Patrick Nelson. a patch has been released You can download the patch here . Update 3 8/16/2016: Latest vs 2015 update 3 KB3165756 has the same problem do not update Version Used: Latest stable version on visual studio update 2 rc Disabled all my extensions and also restarted my all settings Steps to Reproduce: Install visual studio update 2 rc Create new project using asp.net mvc core template Debug the app Expected Behavior: To be able to debug the asp

Can not delete \\bin\\roslyn\\VBCSCompiler.exe - Access is denied

醉酒当歌 提交于 2019-12-02 16:06:12
I am facing a strange issue from roslyn compiler. Sometimes when I build the solution I face a strange issue in error list which does not let me build the solution. Here is the error: Severity Code Description Project File Line Suppression State Error Unable to copy file "D:\Sealogical New Website\SealogicalWebsite\packages\Microsoft.Net.Compilers.1.0.0\tools\csc.exe" to "bin\roslyn\csc.exe". Access to the path 'bin\roslyn\csc.exe' is denied. SealogicalWebsite Severity Code Description Project File Line Suppression State Error Unable to copy file "D:\Sealogical New Website\SealogicalWebsite

In which language is the C# compiler written?

こ雲淡風輕ζ 提交于 2019-12-02 15:45:04
I looked at the source code at http://referencesource.microsoft.com/ , and it appears all the source code is in C#. I also looked at the source code for the new C# compiler platform ( Roslyn ), and it is also in C#. How is that possible? Is C# language compiler written in C#? Or am I missing something obvious? If C# compiler is written in C# then how does it work? The original C# compiler wasn't written in C#, it was in C and C++. The new Roslyn compiler was written in C#, but was initially compiled with the old compiler. Once the new compiler was done, it was able to compile its own source

Roslyn failed to compile code

时光总嘲笑我的痴心妄想 提交于 2019-12-02 14:46:46
After I have migrated my project from VS2013 to VS2015 the project no longer builds. A compilation error occurs in the following LINQ statement: static void Main(string[] args) { decimal a, b; IEnumerable<dynamic> array = new string[] { "10", "20", "30" }; var result = (from v in array where decimal.TryParse(v, out a) && decimal.TryParse("15", out b) && a <= b // Error here orderby decimal.Parse(v) select v).ToArray(); } The compiler returns an error: Error CS0165 Use of unassigned local variable 'b' What causes this issue? Is it possible to fix it through a compiler setting? What does cause

Roslyn Check Type of an Attribute

时光毁灭记忆、已成空白 提交于 2019-12-02 14:44:53
问题 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?

Are Roslyn SyntaxNodes reused?

旧街凉风 提交于 2019-12-02 14:00:01
I've been taking a look to Roslyn CTP and, while it solves a similar problem to the Expression tree API , both are immutable but Roslyn does so in a quite different way: Expression nodes have no reference to the parent node, are modified using a ExpressionVisitor and that's why big parts can be reused. Roslyn's SyntaxNode , on the other side, has a reference to its parent, so all the nodes effectively become a block that's impossible to re-use. Methods like Update , ReplaceNode , etc, are provided to make modifications. Where does this end? Document ? Project ? ISolution ? The API promotes a

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

依然范特西╮ 提交于 2019-12-02 12:58:03
问题 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

Roslyn, MSBuildWorkspace compiling a .NetStandard project referencing a .NetStandard project throws “No value for RuntimeMetadataVersion found.”

我的未来我决定 提交于 2019-12-02 07:12:05
Create a solution with the following projects. A .NetStandard2.0 project, name it "1" A .NetStandard2.0 project, name it "2" Add a reference of project 1 in 2 A .Net4.6.1 console project name it "3" and add the nuget Microsoft.CodeAnalysis.CSharp.Workspaces v2.7.0 Paste in the follwing code into project 3 static void Main(string[] args) { string dir = Path.Combine(Environment.CurrentDirectory, "..\\..\\..\\"); string solutionPath = Directory.GetFiles(dir, "*.sln").First(); var msWorkspace = MSBuildWorkspace.Create(); Solution solution = msWorkspace.OpenSolutionAsync(solutionPath).Result;

Can I access the MsBuildWorkspace from within a MsBuild Task class?

送分小仙女□ 提交于 2019-12-02 06:35:51
问题 As far as I understand, Roslyn have introduced the concept of Workspaces. One implementation of Workspaces is the MsBuildWorkspace. My question is, can I from within a custom build task access a Roslyn Workspace representing the project being built? I suspect that this is the purpose of MsBuildWorkspace. If so, can I access this workspace from the Execute method in my custom task (derived from Microsoft.Build.Utilities.Task )? In case you are wondering why, I need to traverse other aspects of