Last active
August 29, 2015 14:15
-
-
Save jlattimer/5eb0be05a03ebbfb1132 to your computer and use it in GitHub Desktop.
Simple Dynamics CRM code analyzer using the .NET Compiler Platform ("Roslyn")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public override void Initialize(AnalysisContext context) | |
{ | |
context.RegisterSyntaxNodeAction(AnalyzeBal, SyntaxKind.BracketedArgumentList); | |
} | |
private static void AnalyzeBal(SyntaxNodeAnalysisContext context) | |
{ | |
SyntaxNode node = context.Node; | |
if (!node.IsKind(SyntaxKind.BracketedArgumentList)) return; | |
BracketedArgumentListSyntax bal = (BracketedArgumentListSyntax)node; | |
ITypeSymbol type = null; | |
ElementAccessExpressionSyntax parent = (ElementAccessExpressionSyntax)bal.Parent; | |
if (!parent.Expression.IsKind(SyntaxKind.IdentifierName)) return; | |
IdentifierNameSyntax identifier = (IdentifierNameSyntax)parent.Expression; | |
if (identifier != null) | |
type = context.SemanticModel.GetTypeInfo(identifier).Type; | |
if (type == null) return; | |
//If type is not Microsoft.Xrm.Sdk.Entity - exit | |
if (type.ToString() != "Microsoft.Xrm.Sdk.Entity") return; | |
//If arguement does not contain an upper case letter - exit | |
if (!bal.Arguments.ToString().ToCharArray().Any(char.IsUpper)) return; | |
var diagnostic = Diagnostic.Create(Rule, node.GetLocation(), bal.Arguments[0].Expression.GetFirstToken().ValueText); | |
context.ReportDiagnostic(diagnostic); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment