private void AnalyzeMethodRecursive(MethodBase method) { if (this.analyzedMethods.Contains(method)) { return; } this.analyzedMethods.Add(method); using (this.context.InContext(() => this.context.Current.CloneWithDifferentMethod(method))) { ISyntaxMethodBody body = this.syntaxService.GetMethodBody(method, SyntaxAbstractionLevel.ExpressionTree); if (body != null) { this.VisitMethodBody(body); } } }
/// <summary> /// Validates an <see cref="Assembly" /> against the current constraint. /// </summary> /// <param name="target">Parameter to which the current constraint has been applied.</param> /// <param name="assembly">Assembly being validated.</param> public override void ValidateCode(object target, Assembly assembly) { var parameter = (ParameterInfo)target; var resourceManager = new ResourceManager(_resourceBaseName, assembly); // Get the list of methods referencing the parent method of the parameter. var reflectionService = PostSharpEnvironment.CurrentProject.GetService <ISyntaxReflectionService>(); var usages = ReflectionSearch.GetMethodsUsingDeclaration(parameter.Member); foreach (MethodUsageCodeReference usage in usages) { // Decompiles the method into expression trees. ISyntaxMethodBody methodBody = reflectionService.GetMethodBody(usage.UsingMethod, SyntaxAbstractionLevel.ExpressionTree); // Visit the method body. var visitor = new Visitor(parameter, resourceManager); visitor.VisitMethodBody(methodBody); } }
public override bool CompileTimeValidate(Assembly assembly) { ISyntaxReflectionService reflectionService = PostSharpEnvironment.CurrentProject.GetService <ISyntaxReflectionService>(); MethodInfo[] validatedMethods = new[] { typeof(FileExtensionAttributeHelper).GetMethod("GetFileExtensions", BindingFlags.Public | BindingFlags.Static), typeof(FileExtensionAttributeHelper).GetMethod("GetPrimaryFileExtension", BindingFlags.Public | BindingFlags.Static) }; MethodBase[] referencingMethods = validatedMethods .SelectMany(ReflectionSearch.GetMethodsUsingDeclaration) .Select(r => r.UsingMethod) .Where(m => !validatedMethods.Contains(m)) .Distinct() .ToArray(); foreach (MethodBase userMethod in referencingMethods) { ISyntaxMethodBody body = reflectionService.GetMethodBody(userMethod, SyntaxAbstractionLevel.ExpressionTree); ValidateMethodBody(body, userMethod, validatedMethods); } return(false); }
private void ValidateMethodBody(ISyntaxMethodBody methodBody, MethodBase userMethod, MethodInfo[] validatedMethods) { MethodBodyValidator validator = new MethodBodyValidator(userMethod, validatedMethods); validator.VisitMethodBody(methodBody); }