public override void Process([NotNull] IProcessableComponent component) { if (component.Type != ProcessableComponentType.MethodParameter) { throw new InvalidOperationException("Component is expected to be method parameter."); } var parameter = (MethodParameterProcessableComponent)component; ModuleData moduleData; if (!modulesData.TryGetValue(parameter.DeclaringModule, out moduleData)) { moduleData = new ModuleData(); var codeProvider = Configuration.CustomValueHandlingCodeProvider; moduleData.CodeInjector = new CodeInjector <ParameterValueHandlingCodeProviderArgument>(parameter.DeclaringModule, codeProvider); moduleData.StateHoldingField = PrepareStateHoldingField(codeProvider, parameter.DeclaringModule); modulesData.Add(parameter.DeclaringModule, moduleData); } var codeProviderArgument = new ParameterValueHandlingCodeProviderArgument(parameter, moduleData.StateHoldingField); if (moduleData.CodeInjector.ShouldBeCallInjected(codeProviderArgument)) { moduleData.CodeInjector.InjectAtBegining(parameter.DeclaringComponent.UnderlyingComponent, codeProviderArgument, Logger); } }
public override void Process([NotNull] IProcessableComponent component) { if (component.Type != ProcessableComponentType.Method) { throw new InvalidOperationException("Component is expected to be method."); } var method = (MethodProcessableComponent)component; var declaringModule = method.DeclaringModule; ModuleData moduleData; if (!modulesData.TryGetValue(declaringModule, out moduleData)) { moduleData = new ModuleData(); var codeProvider = Configuration.CodeProvider; var stateInstanceName = GetStateInstanceName(method); moduleData.CodeInjector = new CodeInjector <MethodCodeInjectingCodeProviderArgument>(declaringModule, codeProvider); moduleData.StateHoldingField = PrepareStateHoldingField(stateInstanceName, codeProvider, declaringModule); modulesData.Add(declaringModule, moduleData); } var codeProviderArgument = new MethodCodeInjectingCodeProviderArgument(method, moduleData.StateHoldingField); if (moduleData.CodeInjector.ShouldBeCallInjected(codeProviderArgument)) { var injectionPlace = GetInjectionPlace(method); switch (injectionPlace) { case MethodInjectionPlace.Begining: moduleData.CodeInjector.InjectAtBegining(method.UnderlyingComponent, codeProviderArgument, Logger, injectionPlace); break; case MethodInjectionPlace.Exit: moduleData.CodeInjector.InjectBeforeExit(method.UnderlyingComponent, codeProviderArgument, Logger, injectionPlace); break; case MethodInjectionPlace.InCatchBlock: moduleData.CodeInjector.InjectInCatchBlock(method.UnderlyingComponent, codeProviderArgument, Logger, injectionPlace); break; case MethodInjectionPlace.InFinallyBlock: throw new InvalidOperationException($"Under construction injection place '{injectionPlace}' specified."); case MethodInjectionPlace.Custom: if (CustomInstructionsInjection == null) { throw new InvalidOperationException($"If you want to use {nameof(MethodInjectionPlace)}.{nameof(MethodInjectionPlace.Custom)} you need to set value to {nameof(CustomInstructionsInjection)} property."); } moduleData.CodeInjector.Inject(method.UnderlyingComponent, codeProviderArgument, Logger, injectionPlace, CustomInstructionsInjection); break; default: throw new InvalidOperationException($"Unknown injection place '{injectionPlace}' specified."); } } }
public override IEnumerable <Type> GetAttributeMapping(IProcessableComponent component) { var CurrentAttributeTypes = new List <Type>(); var rsites = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable() where t.AttributeType.FullName == typeof(ExceptionRaiseSiteAttribute).FullName select t; var matchRaiseSite = from t in rsites where t.ConstructorArguments.Any(item => item.Value.ToString().Equals(String.Concat(component.DeclaringComponent.Name, ".", component.Name))) select t; var raiseSiteArguments = from t in matchRaiseSite select t.ConstructorArguments.ToArray(); foreach (var raisesite in raiseSiteArguments) { var raisesitename = raisesite[0].Value; CurrentAttributeTypes.Add(typeof(ExceptionRaiseSiteAttribute)); var channels = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable() where t.AttributeType.FullName == typeof(ExceptionChannelAttribute).FullName select t; var channelsByRaiseSite = from t in channels where t.ConstructorArguments.Any(v => v.Value.ToString().Equals(raisesitename)) select t; foreach (var channel in channelsByRaiseSite) { CurrentAttributeTypes.Add(typeof(ExceptionChannelAttribute)); } } var handlers = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable() where t.AttributeType.FullName == typeof(ExceptionHandlerAttribute).FullName select t; var matchHandlers = from t in handlers where t.ConstructorArguments.Any(item => item.Value.ToString().Equals(String.Concat(component.DeclaringComponent.Name, ".", component.Name))) select t; foreach (var item in matchHandlers) { CurrentAttributeTypes.Add(typeof(ExceptionHandlerAttribute)); } return(CurrentAttributeTypes); }
public override void Process([NotNull] IProcessableComponent component) { if (component.Type != ProcessableComponentType.Module) { throw new InvalidOperationException("Component is expected to be module."); } var module = (ModuleProcessableComponent)component; var attributes = module.CustomAttributes.Where(a => a.AttributeType.FullName == ModuleInitializerAttributeFullName); foreach (var attribute in attributes) { ProcessModuleInitializerAttribute(attribute, module); } }
protected override Type GetAttributeType(IProcessableComponent component, Type attr) { if (component.Name.StartsWith(AddAttributeProcessorTests.InjectAttribute1Prefix)) { return(typeof(Injected1Attribute)); } else if (component.Name.StartsWith(AddAttributeProcessorTests.InjectAttribute2Prefix)) { return(typeof(Injected2Attribute)); } else { throw new InvalidOperationException(); } }
public void AddAttributeFromProvider(IProcessableComponent component, ILogger logger) { foreach (var customAttributeType in attributeProvider.GetAttributeMapping(component)) { var attributeInfo = attributeProvider.GetAttributeInfo(component, customAttributeType); if (!attributeInfo.ShouldBeAttributeInjected) { return; } logger.Notice($"Injecting attribute to {component.FullName}."); component.CustomAttributes.Add(attributeInfo.CustomAttribute); } }
private static void ProcessComponent( IProcessableComponent component, IEnumerable <IComponentProcessor <ComponentProcessorConfiguration> > componentProcessors, ILogger logger) { foreach (var processor in componentProcessors) { try { processor.Process(component); } catch (Exception e) { logger.Error($"There was an error while processing '{component.FullName}' with processor '{processor}'. CurrentDir: '{Environment.CurrentDirectory}'. Exception: {e}"); throw new Exception(); } } }
public AttributeProviderInjectionInfo GetAttributeInfo(IProcessableComponent component, Type att) { if (ShouldBeInjected(component, att)) { var attributeArguments = GetAttributeArguments(component, att); if (attributeArguments == null) { attributeArguments = new AttributeProviderAttributeArgument[0]; } var attributeType = GetAttributeType(component, att); var attribute = GetAndCheckCustomAttribute(attributeType, attributeArguments, component.DeclaringModule); return(new AttributeProviderInjectionInfo(true, attribute)); } else { return(new AttributeProviderInjectionInfo(false, null)); } }
protected override Type GetAttributeType(IProcessableComponent component, Type attr) { if (attr.Name.Equals(typeof(ExceptionRaiseSiteAttribute).Name)) { return(typeof(ExceptionRaiseSiteAttribute)); } else if (attr.Name.Equals(typeof(ExceptionChannelAttribute).Name)) { return(typeof(ExceptionChannelAttribute)); } else if (attr.Name.Equals(typeof(ExceptionHandlerAttribute).Name)) { return(typeof(ExceptionHandlerAttribute)); } else if (attr.Name.Equals(typeof(ExceptionInterfaceAttribute).Name)) { return(typeof(ExceptionInterfaceAttribute)); } else { throw new ArgumentException(); } }
public override void Process([NotNull] IProcessableComponent component) { if (component.Type != ProcessableComponentType.MethodParameter) { throw new InvalidOperationException("Component is expected to be method parameter."); } var parameter = (MethodParameterProcessableComponent)component; var parameterDefinition = parameter.UnderlyingComponent; if (parameter.CustomAttributes.Any(a => a.AttributeType.FullName == NotNullAttributeFullName)) { var methodDefinition = parameter.DeclaringComponent.UnderlyingComponent; if (parameterDefinition.ParameterType.IsValueType && !parameterDefinition.ParameterType.IsNullableValueType()) { Logger.LogErrorWithSource(methodDefinition, $"Parameter '{parameter.Name}' of method '{methodDefinition.FullName}' cannot be non-nullable because it is a value type."); } else { base.Process(parameter); } } }
protected abstract AttributeProviderAttributeArgument[] GetAttributeArguments(IProcessableComponent component, Type attr);
protected abstract Type GetAttributeType(IProcessableComponent component, Type attr);
protected abstract bool ShouldBeInjected(IProcessableComponent component, Type attr);
public abstract IEnumerable <Type> GetAttributeMapping(IProcessableComponent component);
protected override bool ShouldBeInjected(IProcessableComponent component, Type attr) { bool ShouldBeInjected = false; CurrentAttributeArguments = new ArrayList(); if (attr.Name.Equals(typeof(ExceptionRaiseSiteAttribute).Name)) { var rsites = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable() where t.AttributeType.FullName == typeof(ExceptionRaiseSiteAttribute).FullName select t; var matchRaiseSite = from t in rsites where t.ConstructorArguments.Any(item => item.Value.ToString().Equals(String.Concat(component.DeclaringComponent.Name, ".", component.Name))) select t; var raiseSiteArguments = from t in matchRaiseSite select t.ConstructorArguments.ToArray(); foreach (var item in raiseSiteArguments) { for (int i = 0; i < item.Length; i++) { CurrentAttributeArguments.Add(item[i].Value); } ShouldBeInjected = true; } } else if (attr.Name.Equals(typeof(ExceptionChannelAttribute).Name)) { var rsites = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable() where t.AttributeType.FullName == typeof(ExceptionRaiseSiteAttribute).FullName select t; var matchRaiseSite = from t in rsites where t.ConstructorArguments.Any(item => item.Value.ToString().Equals(String.Concat(component.DeclaringComponent.Name, ".", component.Name))) select t; var raiseSiteArguments = from t in matchRaiseSite select t.ConstructorArguments.ToArray(); foreach (var raisesite in raiseSiteArguments) { var raisesitename = raisesite[0].Value; var channels = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable() where t.AttributeType.FullName == typeof(ExceptionChannelAttribute).FullName select t; var channelsByRaiseSite = from t in channels where t.ConstructorArguments.Any(v => v.Value.ToString().Equals(raisesitename)) select t; var channelArguments = from t in channelsByRaiseSite select t.ConstructorArguments.ToArray(); foreach (var item in channelArguments) { for (int i = 0; i < item.Length; i++) { CurrentAttributeArguments.Add(item[i].Value); } ShouldBeInjected = true; } } } else if (attr.Name.Equals(typeof(ExceptionHandlerAttribute).Name)) { var handlers = from t in Assembly.GetExecutingAssembly().CustomAttributes.AsQueryable() where t.AttributeType.FullName == typeof(ExceptionHandlerAttribute).FullName select t; var matchHandlers = from t in handlers where t.ConstructorArguments.Any(item => item.Value.ToString().Equals(String.Concat(component.DeclaringComponent.Name, ".", component.Name))) select t; var handlersArguments = from t in matchHandlers select t.ConstructorArguments.ToArray(); foreach (var item in handlersArguments) { for (int i = 0; i < item.Length; i++) { CurrentAttributeArguments.Add(item[i].Value); } ShouldBeInjected = true; } } else if (attr.Name.Equals(typeof(ExceptionInterfaceAttribute).Name)) { throw new ArgumentException(); } else { throw new ArgumentException(); } return(ShouldBeInjected); }
protected override AttributeProviderAttributeArgument[] GetAttributeArguments(IProcessableComponent component, Type att) { return(new AttributeProviderAttributeArgument[] { AttributeProviderAttributeArgument.CreateParameterArgument("text", $"hello from '{component.Name}'!") }); }
protected override bool ShouldBeInjected(IProcessableComponent component, Type att) { return(component.Type == ProcessableComponentType.Assembly); }
protected override AttributeProviderAttributeArgument[] GetAttributeArguments(IProcessableComponent component, Type attr) { var typeDefinition = component.Type == ProcessableComponentType.Type ? ((TypeDefinition)component.UnderlyingComponent) : null; if (attr.Name.Equals(typeof(ExceptionRaiseSiteAttribute).Name)) { return(new AttributeProviderAttributeArgument[] { AttributeProviderAttributeArgument.CreateParameterArgument("name", CurrentAttributeArguments[0].ToString()), AttributeProviderAttributeArgument.CreateParameterArgument("target", CurrentAttributeArguments[1].ToString()) }); } else if (attr.Name.Equals(typeof(ExceptionChannelAttribute).Name)) { return(new AttributeProviderAttributeArgument[] { AttributeProviderAttributeArgument.CreateParameterArgument("name", CurrentAttributeArguments[0].ToString()), AttributeProviderAttributeArgument.CreateParameterArgument("exception", CurrentAttributeArguments[1].ToString()), AttributeProviderAttributeArgument.CreateParameterArgument("raiseSite", CurrentAttributeArguments[2].ToString()) }); } else if (attr.Name.Equals(typeof(ExceptionHandlerAttribute).Name)) { return(new AttributeProviderAttributeArgument[] { AttributeProviderAttributeArgument.CreateParameterArgument("channel", CurrentAttributeArguments[0].ToString()), AttributeProviderAttributeArgument.CreateParameterArgument("target", CurrentAttributeArguments[1].ToString()), AttributeProviderAttributeArgument.CreateParameterArgument("exception", CurrentAttributeArguments[2].ToString()), AttributeProviderAttributeArgument.CreateParameterArgument("methodName", CurrentAttributeArguments[3].ToString()), }); } else if (attr.Name.Equals(typeof(ExceptionInterfaceAttribute).Name)) { return(new AttributeProviderAttributeArgument[] { AttributeProviderAttributeArgument.CreateParameterArgument("name", CurrentAttributeArguments[0].ToString()), AttributeProviderAttributeArgument.CreateParameterArgument("target", CurrentAttributeArguments[1].ToString()) }); } else { throw new ArgumentException(); } }
public override IEnumerable <Type> GetAttributeMapping(IProcessableComponent component) { throw new NotImplementedException(); }
protected override bool ShouldBeInjected(IProcessableComponent component, Type att) { return(component.Name.StartsWith(AddAttributeProcessorTests.InjectAttribute1Prefix) || component.Name.StartsWith(AddAttributeProcessorTests.InjectAttribute2Prefix)); }
public override void Process([NotNull] IProcessableComponent component) { attributeInjector.AddAttributeFromProvider(component, Logger); }
public virtual void Process([NotNull] IProcessableComponent component) { throw new NotImplementedException(); }
protected override Type GetAttributeType(IProcessableComponent component, Type att) { return(typeof(CustomAssemblyInfoAttribute)); }
protected override AttributeProviderAttributeArgument[] GetAttributeArguments(IProcessableComponent component, Type attr) { var typeDefinition = component.Type == ProcessableComponentType.Type ? ((TypeDefinition)component.UnderlyingComponent) : null; if (component.Name.StartsWith(AddAttributeProcessorTests.InjectAttribute1Prefix)) { return(new AttributeProviderAttributeArgument[] { AttributeProviderAttributeArgument.CreateParameterArgument("component", component.Type), AttributeProviderAttributeArgument.CreateParameterArgument("type", typeDefinition), AttributeProviderAttributeArgument.CreateParameterArgument("nameHash", component.Name.GetHashCode()), AttributeProviderAttributeArgument.CreateParameterArgument("name", component.Name) }); } else if (component.Name.StartsWith(AddAttributeProcessorTests.InjectAttribute2Prefix)) { return(new AttributeProviderAttributeArgument[] { AttributeProviderAttributeArgument.CreateParameterArgument("component", new Enum[] { component.Type, component.Type, component.Type }), AttributeProviderAttributeArgument.CreateParameterArgument("type", new TypeReference[] { typeDefinition, typeDefinition, typeDefinition }), AttributeProviderAttributeArgument.CreateParameterArgument("nameHash", new int[] { component.Name.GetHashCode(), component.Name.GetHashCode(), component.Name.GetHashCode() }), AttributeProviderAttributeArgument.CreateParameterArgument("name", new string[] { component.Name, component.Name, component.Name }) }); } else { throw new InvalidOperationException(); } }