Tag: roslyn

Roslyn加载项目文档失败

在Visual Studio扩展(VSIX)解决方案中,我使用Roslyn从当前解决方案加载特定项目: Project myProject = this.CurrentComponentModel.GetService() .CurrentSolution.Projects .FirstOrDefault(p => p.Name == “MyProject”) 项目myProject肯定是加载的,但在检查时我看到: myProject.HasDocuments == false myProject.Documents is Empty 然而,在Visual Studio中,我可以看到大量文档。 如果我关闭解决方案并从另一个TFS分支打开相同的解决方案,则相同的代码返回: myProject.HasDocuments == true myProject.Documents is not Empty 有任何想法吗?

Roslyn:如何使用Roslyn C获取DeclarationSyntax的命名空间#

我有一个包含一些类文件的ac#解决方案。 通过Roslyn,我能够解析解决方案以获得解决方案中的项目列表。 从那里,我可以获得每个项目的文件。 然后,我可以获得每个ClassDeclarationSyntax的列表。 这是起点。 foreach (var v in _solution.Projects) { //Console.WriteLine(v.Name.ToString()); foreach (var document in v.Documents) { SemanticModel model = document.GetSemanticModelAsync().Result; var classes = document.GetSyntaxRootAsync().Result.DescendantNodes().OfType(); foreach(var cl in classes) { // Starting around this point… ClassDiagramClass cls = new ClassDiagramClass(cl, model); diagramClasses.Add(cls); } } } 从这些对象我希望能够获得每个类中使用的变量的命名空间。 参见文件1有一个方法“getBar()”,它返回一个B.Bar类型的对象。 命名空间很重要,因为它告诉您实际返回的是哪种类型的Bar。 File1.cs using B; namespace A { […]

如何在多个脚本的批处理中使用Roslyn C#脚本?

我正在编写multithreading解决方案,用于将数据从不同的源传输到中央数据库。 解决方案通常包含两部分: 单线程导入引擎 在线程中调用Import引擎的multithreading客户端。 为了最大限度地减少自定义开发,我使用的是Roslyn脚本。 导入引擎项目中的Nuget包管理器启用了此function。 每个导入都被定义为输入表的转换 – 具有输入字段的集合 – 再次转换为目标表 – 再次使用目标字段的集合。 此处使用脚本引擎允许输入和输出之间的自定义转换。 对于每个输入/输出对,都有带自定义脚本的文本字段。 以下是用于脚本初始化的简化代码: //Instance of class passed to script engine _ScriptHost = new ScriptHost_Import(); if (Script != “”) //Here we have script fetched from DB as text { try { //We are creating script object … ScriptObject = CSharpScript.Create(Script, globalsType: typeof(ScriptHost_Import)); //… […]

使用Roslyn 2012年6月CTP更改Visual Studio 2012 RC中C#Interactive窗口的字体和颜色

默认设置适用于Light Theme,但在暗设置下,默认消息文本保持黑色(实际上错误消息文本颜色也保留,但它是红色,所以没关系)。 基本上它看起来与相关问题完全一样,但在这种情况下我使用的是VS 2012。 有关 有没有办法在Roslyn CTP中更改C#Interactive窗口的字体和颜色?

Roslyn重命名Majusucle中的变量const

试图转换: const string maj = “variable”; 在 const string MAJ = “variable”; 我正在使用CodeFix的Diagnostic。 我已经完成了诊断: var localDeclarationConst = node as LocalDeclarationStatementSyntax; if (localDeclarationConst != null && localDeclarationConst.Modifiers.Any(SyntaxKind.ConstKeyword) ) { foreach (VariableDeclaratorSyntax variable in localDeclarationConst.Declaration.Variables) { var symbol = model.GetDeclaredSymbol(variable); if (symbol != null) { string varName = symbol.Name; if (!varName.Equals(varName.ToUpper())) { addDiagnostic(Diagnostic.Create(Rule, localDeclarationConst.GetLocation(), “Les constantes doivent […]

如何使用roslyn将现有项目添加到c#解决方案?

我想将一个现有项目添加到我的c#solution.I使用TryApplyChanges并返回true,但它不会保存对真实解决方案的更改。 我使用的是Microsoft.CodeAnalysis。* 1.1.1版。 public void AddProject (string solutionName) { MSBuildWorkspace ws=MSBuildWorkspace.Create (); ws.OpenSolutionAsync (solutionName); ws.OpenProjectAsync (“ProjectName”); if (ws.TryApplyChanges (ws.CurrentSolution ) {// break point is here } }

使用Roslyn来解析类函数和属性

我是Roslyn的新手,我正在尝试解析cs文件并获取函数和属性。 API示例 //won’t work any more var tree = SyntaxTree.ParseText(…); var tree = SyntaxTree.ParseFle(…); var root = tree.Root; API似乎已经改变,它将不再起作用。 猜猜它只适用于2012 Roslyn CTP,只看它的外观。 然后我尝试新的例子: //won’t work neither var compilation = CSharpCompilation.Create(“HelloWorld”) .AddReferences( new MetadataFileReference( typeof(object).Assembly.Location)) .AddSyntaxTrees(tree); var tree = CSharpSyntaxTree.ParseText(strSourceCode); var root = tree.GetRoot(); 同样,它将不再起作用,因为MetadataFileReference是我得到的Roslyn版本中的抽象类。 API又改变了吗? 我不确定。 VS&Packages 我在我的公司电脑上使用VS2013 PRO UPDATE 2。 2015年7月5日至今。 不,我没有配备VS2015的电脑,我不确定这是否重要。 我打开一个新的WinForm项目,然后在NuGet PM中按照https://github.com/dotnet/roslyn的指示安装了Roslyn。 […]

Roslyn Analyzer与单独的类文件进行比较

我正在编写一个Roslyn分析器,它比较检查const是否已在Localization Resx中声明。 public const string DiagnosticId = “LocalizationTool”; // You can change these strings in the Resources.resx file. If you do not want your analyzer to be localize-able, you can use regular strings for Title and MessageFormat. // See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/Localizing%20Analyzers.md for more on localization private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.AnalyzerTitle), Resources.ResourceManager, typeof(Resources)); private […]

使用Roslyn更改文件

我正在尝试编写一个命令行工具,使用Roslyn修改一些代码。 一切似乎都顺利:打开解决方案,更改解决方案,Workspace.TryApplyChanges方法返回true。 但是,磁盘上没有更改实际文件。 这是怎么回事? 下面是我正在使用的顶级代码。 static void Main(string[] args) { var solutionPath = args[0]; UpdateAnnotations(solutionPath).Wait(); } static async Task UpdateAnnotations(string solutionPath) { using (var workspace = MSBuildWorkspace.Create()) { var solution = await workspace.OpenSolutionAsync(solutionPath); var newSolution = await SolutionAttributeUpdater.UpdateAttributes(solution); var result = workspace.TryApplyChanges(newSolution); Console.WriteLine(result); return result; } }

即使Roslyn Diagnostic Analyzer出现了一些错误,编译仍然成功

DiagnosticAnalyzer是一个基于Roslyn的自定义扩展,其DiagnosticDescriptor与DiagnosticSeverity.Error如下所示 internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Description, MessageFormat, Category, DiagnosticSeverity.Error); 当使用扩展时,它确实显示了针对诊断分析器自定义规则的代码的红色波形,强调它是一个错误 ,它甚至显示在Visual Studio的错误列表窗口中。 但是在编译时 ,此代码在Visual Studio的输出窗口中获取编译成功消息。 这违背了将错误强调为诊断分析器严重性的整个概念。 如果这是Roslyn的默认工作方式,那么我们是否有办法停止编译 。 我正在使用: Visual Studio 2013 Update 3 Roslyn最终用户Preview.vsix 包含使用Roslyn SDK Project Templates.vsix制作的DiagnosticAnalyzer的VSIX扩展