roslyn

Finding all class declarations than inherit from another with Roslyn

我的梦境 提交于 2020-01-19 12:54:17
问题 I have a CSharpCompilation instance containing an array of SyntaxTree s and I am trying to find all the class declarations that inherit from a class e.g // Not in syntax tree but referenced in project public class Base{} // In syntax tree, how to find all such classes? public class MyClass : Base {} I've tried a few things but am a bit confused with all the options and can't seem to find the right way to do this. I've tried to get the symbols but this doesn't work for inherited types

Mark parameters with a [ReadOnly] attribute and let an analyzer assert, that no reassignment is in the method body

断了今生、忘了曾经 提交于 2020-01-17 15:27:29
问题 Not being able to mark a parameter of a method as readonly so that it is impossible to be reassigned in the method, I started thinking abount creating an analyzer for this. The parameter would be attributed with [AttributeUsage(AttributeTargets.Parameter)] public class ReadOnlyAttribute: Attribute { // ... } and the signature of a method would be public class Foo { public void Bar([ReadOnly] MyObject o) { o.DoSomething() // this is OK o = new MyObject() // this would be flagged by the

When to use SemanticModel.GetSymbolInfo and when SemanticModel.GetDeclaredSymbol

℡╲_俬逩灬. 提交于 2020-01-14 07:04:50
问题 In some cases, when I'm trying to get the the ISymbol for my syntax node, I'm fail (getting null) when using SemanticModel.GetSymbolInfo but succeed when using SemanticModel.GetDeclaredSymbol. I've attached an example bellow. So my question is when to use each one of the methods for getting the semantic model? public class Class1 { public System.String MyString { get; set; } public static void Main() { var str = @" namespace ClassLibrary31 { public class Class1 { public System.String MyString

Change files using Roslyn

吃可爱长大的小学妹 提交于 2020-01-14 03:45:08
问题 I'm trying to write a command line tool that modifies some code using Roslyn. Everything seems to go well: the solution is opened, the solution is changed, the Workspace.TryApplyChanges method returns true. However no actual files are changed on disk. What's up? Below is the top level code I'm using. static void Main(string[] args) { var solutionPath = args[0]; UpdateAnnotations(solutionPath).Wait(); } static async Task<bool> UpdateAnnotations(string solutionPath) { using (var workspace =

csc.exe exited with code -2146232797 vs 2017

最后都变了- 提交于 2020-01-13 17:37:32
问题 I have error in my project on time of build csc.exe exited with code -2146232797 visual studio 2017(Professional) in other Team's computer project building and running fine, so I not want to update nuget compiler, Is there any permanent solutions there? 回答1: This issue happened for some people with vs 2017. you need to reinstall .net via microsoft site as requested and if the above link does not help, there is a thread in visual studio development community stating they fixed the issue in

csc.exe exited with code -2146232797 vs 2017

六月ゝ 毕业季﹏ 提交于 2020-01-13 17:37:00
问题 I have error in my project on time of build csc.exe exited with code -2146232797 visual studio 2017(Professional) in other Team's computer project building and running fine, so I not want to update nuget compiler, Is there any permanent solutions there? 回答1: This issue happened for some people with vs 2017. you need to reinstall .net via microsoft site as requested and if the above link does not help, there is a thread in visual studio development community stating they fixed the issue in

How to use Roslyn C# scripting in batch processing with several scripts?

天大地大妈咪最大 提交于 2020-01-13 16:29:13
问题 I am writing multi-threaded solution that will be used for transferring data from different sources to a central database. Solution, in general, has two parts: Single-threaded Import engine Multi-threaded client that invokes Import engine in threads. In order to minimize custom development I am using Roslyn scripting. This feature is enabled with Nuget Package manager in Import engine project. Every import is defined as transformation of input table – that has collection of input fields – to

New C# 6 object initializer syntax?

拜拜、爱过 提交于 2020-01-13 09:03:51
问题 I just noticed that the following is possible in C# written in Visual Studio 2015, but I've never seen it before: public class X { public int A { get; set; } public Y B { get; set; } } public class Y { public int C {get; set; } } public void Foo() { var x = new X { A = 1, B = { C = 3 } }; } My expectation was for Foo to have to be implemented like this: public void Foo() { var x = new X { A = 1, B = new Y { C = 3 } }; } Note that there is no need to call new Y . Is this new in C# 6? I haven't

New C# 6 object initializer syntax?

大兔子大兔子 提交于 2020-01-13 09:03:10
问题 I just noticed that the following is possible in C# written in Visual Studio 2015, but I've never seen it before: public class X { public int A { get; set; } public Y B { get; set; } } public class Y { public int C {get; set; } } public void Foo() { var x = new X { A = 1, B = { C = 3 } }; } My expectation was for Foo to have to be implemented like this: public void Foo() { var x = new X { A = 1, B = new Y { C = 3 } }; } Note that there is no need to call new Y . Is this new in C# 6? I haven't

Find all method calls for a specific method using Roslyn

大憨熊 提交于 2020-01-13 05:18:46
问题 I am working on a code analyser using Roslyn and my current task is to find all internal methods which are unused in the assembly. I start with a MethodDeclarationSyntax and get the symbol from that. I then use the FindCallersAsync method in SymbolFinder , but it returns an empty collection even when I am making a call to the method in question somewhere in the assembly. See the code below. protected override void Analyze(SyntaxNodeAnalysisContext context) { NodeToAnalyze = context.Node; var