{"id":596,"date":"2015-10-11T22:17:54","date_gmt":"2015-10-11T13:17:54","guid":{"rendered":"http:\/\/www.biscrat.com\/blog\/?p=596"},"modified":"2024-03-19T03:32:29","modified_gmt":"2024-03-18T18:32:29","slug":"roslyn-code-analyzer","status":"publish","type":"post","link":"https:\/\/biscrat.com\/blog\/roslyn-code-analyzer\/","title":{"rendered":"Roslyn\u306eCode Analyzer\u3092\u4f5c\u3063\u3066\u307f\u308b\u30e1\u30e2"},"content":{"rendered":"<p>Visual Studio 2015\u3067\u306fAnalyze\u306a\u308b\u3082\u306e\u3092\u81ea\u4f5c\u3057\u3066\u72ec\u81ea\u306e\u30a8\u30e9\u30fc\u3084\u8b66\u544a\u3092\u51fa\u305b\u307e\u3059\u3002<a href=\"https:\/\/www.youtube.com\/watch?v=pPCF5IDIIfM\">Metro.cs #1<\/a>\u3067\u305d\u3093\u306a\u8a71\u3092\u3057\u3066\u3044\u305f\u306e\u3067\u8a66\u3057\u3066\u307f\u305f\u3002<\/p>\n<h2>\u53c2\u8003\u8cc7\u6599<\/h2>\n<p><a href=\"https:\/\/neue.cc\/2014\/11\/20_485.html\">VS2015\u306eRoslyn\u3067Code Analyzer\u3092\u81ea\u4f5c\u3059\u308b(\u3064\u3044\u3067\u306bUnity\u30b3\u30fc\u30c9\u3082\u89e3\u6790\u3059\u308b)<\/a><br \/>\n<a href=\"https:\/\/www.buildinsider.net\/enterprise\/roslyn\/02\">\u65b0\u3057\u3044\u30b3\u30f3\u30d1\u30a4\u30e9\u30fc\u201cRoslyn\u201d\u3092\u7528\u3044\u305f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u4f53\u9a13\uff01<\/a><br \/>\n<a href=\"https:\/\/learn.microsoft.com\/ja-jp\/archive\/msdn-magazine\/2014\/special-issue\/csharp-and-visual-basic-use-roslyn-to-write-a-live-code-analyzer-for-your-api\">Roslyn \u3092\u4f7f\u7528\u3057\u305f API \u5411\u3051\u30e9\u30a4\u30d6 \u30b3\u30fc\u30c9 \u30a2\u30ca\u30e9\u30a4\u30b6\u30fc\u306e\u4f5c\u6210<\/a><br \/>\n<a href=\"https:\/\/learn.microsoft.com\/ja-jp\/archive\/msdn-magazine\/2015\/february\/csharp-adding-a-code-fix-to-your-roslyn-analyzer\">Roslyn \u30a2\u30ca\u30e9\u30a4\u30b6\u30fc\u3078\u306e\u30b3\u30fc\u30c9\u4fee\u6b63\u306e\u8ffd\u52a0<\/a><br \/>\n<a href=\"https:\/\/johnkoerner.com\/csharp\/working-with-types-in-your-analyzer\/\">Working with Types in Your Analyzer<\/a><\/p>\n<p>\u5fc5\u8981\u306a\u60c5\u5831\u63a2\u3059\u306e\u306b\u304b\u306a\u308a\u6642\u9593\u304b\u304b\u3063\u305f\u3002\u691c\u7d22\u3059\u308b\u3068\u307b\u3068\u3093\u3069\u82f1\u8a9e\u3067\u51fa\u3066\u304d\u3066\u5927\u5909\u3002\u4e0a4\u3064\u306f\u8cb4\u91cd\u306a\u65e5\u672c\u8a9e\u60c5\u5831\u3067\u307e\u3068\u307e\u3063\u3066\u3044\u308b\u306e\u3067\u8aad\u3080\u3079\u304d\u3002<\/p>\n<h2>\u6e96\u5099<\/h2>\n<p>Visual Studio 2015\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002\u3053\u306e\u3068\u304d\u5171\u901a\u30c4\u30fc\u30eb\u306eVisual Studio\u62e1\u5f35\u6027\u30c4\u30fc\u30eb\u3082\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3002<br \/>\n\u305d\u308c\u3068<a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=VisualStudioProductTeam.NETCompilerPlatformSDK\">.NET Compiler Platform SDK<\/a>\u3082\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/p>\n<h2>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u308b<\/h2>\n<p>\u30e1\u30cb\u30e5\u30fc\u306e \u30d5\u30a1\u30a4\u30eb>\u65b0\u898f\u4f5c\u6210>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8 \u3092\u958b\u3044\u3066 Visual C#>Extensibility>Analyzer with Code Fix \u3092\u9078\u3093\u3067\u65b0\u898f\u4f5c\u6210\u3059\u308b\u3002<br \/>\n\u305d\u3046\u3059\u308b\u3068\u8aac\u660e\u66f8(ReadMe.txt)\u3068\u30b5\u30f3\u30d7\u30eb(DiagnosticAnalyzer.cs, CodeFixProvicer.cs)\u304c\u5165\u3063\u305f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304c\u81ea\u52d5\u4f5c\u6210\u3055\u308c\u308b\u3002<\/p>\n<h2>\u30b3\u30fc\u30c9\u3092\u66f8\u304f<\/h2>\n<p>DiagnositcAnalyzer.cs\u3067\u554f\u984c\u7b87\u6240\u3092\u898b\u3064\u3051\u3066\u30a8\u30e9\u30fc\u3084\u8b66\u544a\u3092\u51fa\u3059\u3002CodeFixProvider.cs\u3067\u306f\u305d\u306e\u4fee\u6b63\u6848\u3092\u4f5c\u308b\u3002<br \/>\n\u8a73\u3057\u304f\u306f\u53c2\u8003\u8cc7\u6599\u3092\u53c2\u7167\u3002Syntax Visualizer\u3068\u30c7\u30d0\u30c3\u30b0\u5b9f\u884c\u306e\u30a6\u30a9\u30c3\u30c1\u5f0f\u306a\u3069\u3092\u898b\u306a\u304c\u3089\u6c17\u5408\u3044\u3067\u4f55\u3068\u304b\u3059\u308b\u306e\u304c\u30c7\u30d5\u30a9\u3063\u307d\u3044\u3002<br \/>\n\u4eca\u65e5\u306f\u8abf\u3079\u308b\u3060\u3051\u3067\u529b\u5c3d\u304d\u305f\u306e\u3067\u3001\u3068\u308a\u3042\u3048\u305a\u975e\u30b8\u30a7\u30cd\u30ea\u30c3\u30af\u306eIEnumearble\u3092\u898b\u3064\u3051\u305f\u3089\u8b66\u544a\u3092\u51fa\u3059Analyzer\u3092\u66f8\u3044\u305f\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing System.Collections.Immutable;\nusing Microsoft.CodeAnalysis;\nusing Microsoft.CodeAnalysis.CSharp;\nusing Microsoft.CodeAnalysis.CSharp.Syntax;\nusing Microsoft.CodeAnalysis.Diagnostics;\n\nnamespace ForbidIEnumerable\n{\n    &#x5B;DiagnosticAnalyzer(LanguageNames.CSharp)]\n    public class ForbidIEnumerableAnalyzer : DiagnosticAnalyzer\n    {\n        public const string DiagnosticId = &quot;ForbidIEnumerable&quot;;\n\n        private static readonly LocalizableString Title = &quot;Forbit to use IEnumerable&quot;;\n        private static readonly LocalizableString MessageFormat = &quot;IEnumerable is forbidden. Please use IEnumerable&lt;T&gt;.&quot;;\n        private static readonly LocalizableString Description = &quot;IEnumerable error {0}&quot;;\n        private const string Category = &quot;Prohibition&quot;;\n\n        private static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);\n\n        public override ImmutableArray&lt;DiagnosticDescriptor&gt; SupportedDiagnostics\n        { get { return ImmutableArray.Create(Rule); } }\n\n        public override void Initialize(AnalysisContext context)\n        {\n            context.RegisterSyntaxNodeAction(syntaxContext =&gt;\n            {\n                var declarationSyntax = (VariableDeclarationSyntax)syntaxContext.Node;\n                var typeInfo = syntaxContext.SemanticModel.GetTypeInfo(declarationSyntax.Type);\n                if (typeInfo.ConvertedType.SpecialType == SpecialType.System_Collections_IEnumerable) {\n                    syntaxContext.ReportDiagnostic(Diagnostic.Create(Rule, syntaxContext.Node.GetLocation()));\n                }\n            }, SyntaxKind.VariableDeclaration);\n        }\n    }\n}\n<\/pre>\n<h2>\u5b9f\u884c\u3057\u3066\u307f\u308b<\/h2>\n<p>\u30c7\u30d0\u30c3\u30b0\u5b9f\u884c\u3059\u308b\u3068\u30a2\u30ca\u30e9\u30a4\u30b6\u3092\u8aad\u307f\u8fbc\u3093\u3060VisualStudio\u304c\u8d77\u52d5\u3055\u308c\u308b\u306e\u3067\u305d\u308c\u3067\u30c6\u30b9\u30c8\u3067\u304d\u308b\u3002<br \/>\n\u5b8c\u6210\u3057\u305f\u3082\u306e\u3092\u5b9f\u969b\u306b\u4f7f\u3046\u306b\u306f\u7d44\u307f\u8fbc\u3080\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u300c\u53c2\u7167\u300d\u3092\u53f3\u30af\u30ea\u30c3\u30af\u3057\u3066\u300c\u30a2\u30ca\u30e9\u30a4\u30b6\u30fc\u306e\u8ffd\u52a0\u300d\u304b\u3089\u30d3\u30eb\u30c9\u3055\u308c\u305fdll\u3092\u8aad\u307f\u8fbc\u3080\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/biscrat.com\/blog\/wp-content\/uploads\/roslyn-code-analyzer.png\" alt=\"roslyn-code-analyzer\" width=\"606\" height=\"133\" class=\"alignnone size-full wp-image-601\" \/><br \/>\n\u5b9f\u884c\u3057\u3066\u307f\u305f\u3068\u3053\u308d\u3002\u8b66\u544a\u304c\u51fa\u3066\u4f55\u304b\u5909\u306a\u3053\u3068\u3092\u6559\u3048\u3066\u304f\u308c\u308b\u3002\u306a\u305c\u304b\u8b66\u544a\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u4e8c\u91cd\u3067\u51fa\u3066\u3044\u308b\u304c\u539f\u56e0\u306f\u4e0d\u660e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visual Studio 2015\u3067\u306fAnalyze\u306a\u308b\u3082\u306e\u3092\u81ea\u4f5c\u3057\u3066\u72ec\u81ea\u306e\u30a8\u30e9\u30fc\u3084\u8b66\u544a\u3092\u51fa\u305b\u307e\u3059\u3002Metro.cs #1\u3067\u305d\u3093\u306a\u8a71\u3092\u3057\u3066\u3044\u305f\u306e\u3067\u8a66\u3057\u3066\u307f\u305f\u3002 \u53c2\u8003\u8cc7\u6599 VS2015\u306eRoslyn\u3067Code Analy [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[16,18,17],"class_list":["post-596","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-roslyn","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/biscrat.com\/blog\/wp-json\/wp\/v2\/posts\/596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/biscrat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/biscrat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/biscrat.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/biscrat.com\/blog\/wp-json\/wp\/v2\/comments?post=596"}],"version-history":[{"count":18,"href":"https:\/\/biscrat.com\/blog\/wp-json\/wp\/v2\/posts\/596\/revisions"}],"predecessor-version":[{"id":991,"href":"https:\/\/biscrat.com\/blog\/wp-json\/wp\/v2\/posts\/596\/revisions\/991"}],"wp:attachment":[{"href":"https:\/\/biscrat.com\/blog\/wp-json\/wp\/v2\/media?parent=596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/biscrat.com\/blog\/wp-json\/wp\/v2\/categories?post=596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/biscrat.com\/blog\/wp-json\/wp\/v2\/tags?post=596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}