Tag: roslyn

如何使用Roslyn检查属性是否使用自定义属性进行修饰?

我想使用Roslyn分析一个C#类,并打算在被访问属性应用了特定属性时执行某些操作。 我怎样才能在CSharpSyntaxWalker.VisitPropertyDeclaration方法覆盖中执行此操作? 例如,在下面的代码块中,我想知道Date属性是否具有Validation属性,如果是,则IsJDate是true还是false? [Validation(IsJDate=true)] public string Date {get; set;} 初始化: filesPath.ToList().ForEach(csFilePath => { SyntaxTree csSyntaxTree = CSharpSyntaxTree.ParseText(csFileSourceCode); // …. } _compiledCsCodes = CSharpCompilation.Create(“CSClassesAssembly”, csFiles.Select(cs => cs.CSSyntaxTree ), references); foreach (CsFile csFile in csFiles) { csFile.FileSemanticModel = _compiledCsCodes.GetSemanticModel(csFile.FullSyntaxTree); }

ProjectReferences存在时,不会填充项目MetadataReferences

我正在加载MSBuildWorkspace中的解决方案: var msWorkspace = MSBuildWorkspace.Create(); var solution = msWorkspace.OpenSolutionAsync(solutionPath).Result; 没有ProjectReferences的项目显示所有MetadataReferences,包括mscorlib。 具有ProjectReferences的项目具有MetadataReferences的空集合。 由于编译工作,我想编译器平台由于某种原因没有填充这个集合,我想知道为什么? 如果我删除ProjectReferences,则会正确填充MetadataReferences集合。 编辑:诊断包含缺少mscorlib类型(如Object和DateTime)的错误,因此项目似乎无法编译,因为这些缺少引用。

如何指定等同于/ features:strict(of csc.exe)到msbuild.exe或.csproj文件?

介绍 考虑这个简单(和坏)的C#类: 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”); } } } 方法M和L都有严重的问题。 在M ,我们通过提升的==运算符(自DateTime重载operator ==存在)询问非可空结构DateTime是否等于null。 这总是下降,编译器可以在编译时告诉,因此我们有一个无法访问的分支( “Yes” )。 在N我们询问o是否是static class Nullable一个实例,它永远不会是这种情况(注意,静态类Nullable与struct Nullable )。 同样,这是开发人员的错误,并且”Yes”语句无法访问。 在这些情况下,我们确实需要编译时警告(或“警告错误”),对吗? 看起来,通过在用于C#1.0到5.0的旧C#编译器中逐渐累积编译器错误和/或遗漏,预期的编译时警告无法与旧编译器一起出现。 幸运的是,我们现在有Roslyn […]

ObjectCreationExpressionSyntax.Type的SemanticModel.GetTypeInfo()返回null

我试图从ObjectCreationExpressionSyntax对象获取类型信息但失败了。 下面是重现问题的示例(请参阅代码中的“ti.Type为null”): using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.MSBuild; namespace RoslynExample { class Program { static void Main(string[] args) { string solutionPath = @”..\..\..\RoslynExample.sln”; MSBuildWorkspace workspace = MSBuildWorkspace.Create(); Solution solution = workspace.OpenSolutionAsync(solutionPath).Result; foreach (var project in solution.Projects) { if (project.Name == “RoslynBugProject”) { foreach (var document in project.Documents) { var compilationUnit = document.GetSyntaxRootAsync().Result; var […]

获取在另一个程序集/项目中声明的类型的类型信息

所以标题说我试图从使用Roslyn在另一个程序集中声明的类型中获取类型信息。 最初我试图通过手动查看引用的程序集来实现这一点,但意识到我没有名称空间信息。 我期待以下工作: var workSpace = Roslyn.Services.Workspace.LoadSolution(solFilePath); IDocument file = null; IProject proje = null; Compilation compilation = Compilation.Create(“Test”); List trees = new List(); foreach (var proj in workSpace.CurrentSolution.Projects) { foreach (var doc in proj.Documents) { if (doc.FilePath.ToLower() == filePath.ToLower()) { file = doc; proje = proj; } else trees.Add(doc.GetSyntaxTree()); } } var targetTree = […]

如何用roslyn删除我的c#解决方案中的所有源代码注释?

我想在我的C#解决方案中使用Roslyn删除源代码中的所有注释。 但我该怎么做? public void DeleteComment() { var code = File.ReadAllText(“code.cs”); SyntaxTree tree = SyntaxFactory.ParseSyntaxTree(code); ///Delete Comments ? }

Roslyn /查找引用 – 无法正确加载Workspace

我正在尝试编写一些代码来查找任何给定方法的所有方法调用,因为我正在寻找创建一个开源UML序列图表工具。 但是,我遇到了麻烦,越过前几行代码:/ API看起来已经发生了巨大的变化,我似乎无法通过查看代码来推断出正确的用法。 当我做: var workspace = new CustomWorkspace(); string solutionPath = @”C:\Workspace\RoslynTest\RoslynTest.sln”; var solution = workspace.CurrentSolution; 我发现workspace.CurrentSolution有0个项目。 我认为这将与之前的Workspace.LoadSolution( string solutionFile )相当,后者可能会包含解决方案中的任何项目,但我没有找到任何成功的路径。 我非常困惑0.o 如果有人可以提供一些关于我如何使用FindReferences API来识别特定方法的所有调用的额外指导,那将非常感谢! 或者,我会更好地采取静态分析方法吗? 我想支持lambdas,iterator方法和async之类的东西。 ================================================== ================== 编辑 – 以下是基于已接受答案的完整示例: using System.Linq; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.MSBuild; using Microsoft.CodeAnalysis.FindSymbols; using System.Diagnostics; namespace RoslynTest { class Program { static void Main(string[] args) { […]

创建新的Microsoft.CodeAnalysis.CustomWorkspace – 得到了ReflectionTypeLoadException

我尝试在.NET编译器平台(“Roslyn”)样本中创建类似ConsoleClassifier的东西。 (Microsoft.CodeAnalysis v0.7 …)此时我得到一个ReflectionTypeLoadException : CustomWorkspace workspace = new CustomWorkspace(); ReflectionTypeLoadException {“Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.”} LoaderExceptions: FileNotFoundException {“Could not load file or assembly ‘Microsoft.Build, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. A rendszer nem találja a megadott fájlt.”:”Microsoft.Build, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”} […]

Roslyn Analyzer规则不会使构建失败

继MS的本教程之后,我为Roslyn创建了一个分析器。 根据该页面,您可以将规则标记为DiagnosticSeverity.Error ,这将导致构建中断: 在声明“规则”字段的行中,您还可以将要生成的诊断的严重性更新为错误而不是警告。 如果正则表达式字符串不解析,Match方法肯定会在运行时抛出exception,您应该像编译C#编译器错误一样阻止构建。 将规则的严重性更改为DiagnosticSeverity.Error: 内部静态DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId,Title,MessageFormat,Category,DiagnosticSeverity.Error,isEnabledByDefault:true,description:Description); 在我的代码中,我已经或多或少地创建了规则,如下所示: private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, true, helpLinkUri: HelpUrl); 这条规则很好。 它会抛出红线,它会在错误列表中显示消息。 但是,构建成功,我能够成功运行该应用程序。 注意:我已创建此规则以捕获此示例的Thread.Sleep 。 是否需要额外的设置来确保规则中断构建?

Roslyn没有引用System.Runtime

我正在开发一个项目,我们正在使用Roslyn为我们编译一些模板。 现在,当我编译模板时,我在CompileResult.Diagnostics收到了多个错误。 错误是: (21,6): error CS0012: The type ‘System.Attribute’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’. (21,6): error CS0012: The type ‘System.Type’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’. […]