Skip to content

Instantly share code, notes, and snippets.

@jlattimer
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlattimer/5eb0be05a03ebbfb1132 to your computer and use it in GitHub Desktop.
Save jlattimer/5eb0be05a03ebbfb1132 to your computer and use it in GitHub Desktop.
Simple Dynamics CRM code analyzer using the .NET Compiler Platform ("Roslyn")
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