/// <summary> /// Use to ensure certain aspect is used when method is intercepted. /// </summary> /// <param name="aspectType"></param> /// <param name="missingAspectOption"></param> /// <param name="constructorArgs"></param> public RequiredAspectAttribute(Type aspectType, WhenRequiredAspectIsMissing missingAspectOption, params object[] constructorArgs) { if(aspectType == null) throw new ArgumentNullException("aspectType"); if(!aspectType.IsSubclassOf(typeof(Aspect))) throw new ArgumentException("aspectType must be a subclass of the Aspect class."); this.AspectClassType = aspectType; if(missingAspectOption != WhenRequiredAspectIsMissing.DontInstantiate) { // Need to instantiate missing aspect Func<object> rawActivator = aspectType.GetFastActivatorWithEmbeddedArgs(constructorArgs); if(rawActivator != null) { this.activator = () => (Aspect)rawActivator(); } else { // No activator found string strErrorMsg = "Unable to find {0}({1}) constructor necessary to instantiate required missing aspect {0}." .SmartFormat(aspectType.FormatCSharp(), string.Join(", ", constructorArgs.Select(arg => arg == null ? "[ANY]" : arg.GetType().FormatCSharp())) ); throw new ArgumentException(strErrorMsg, "aspectType"); } } this.InstantiateIfMissing = missingAspectOption; }
/// <summary> /// Use to ensure certain aspect is used when method is intercepted. /// </summary> /// <param name="aspectType"></param> /// <param name="missingAspectOption"></param> /// <param name="constructorArgs"></param> public RequiredAspectAttribute(Type aspectType, WhenRequiredAspectIsMissing missingAspectOption, params object[] constructorArgs) { if (aspectType == null) { throw new ArgumentNullException("aspectType"); } if (!aspectType.IsSubclassOf(typeof(Aspect))) { throw new ArgumentException("aspectType must be a subclass of the Aspect class."); } this.AspectClassType = aspectType; if (missingAspectOption != WhenRequiredAspectIsMissing.DontInstantiate) { // Need to instantiate missing aspect Func <object> rawActivator = aspectType.GetFastActivatorWithEmbeddedArgs(constructorArgs); if (rawActivator != null) { this.activator = () => (Aspect)rawActivator(); } else { // No activator found string strErrorMsg = "Unable to find {0}({1}) constructor necessary to instantiate required missing aspect {0}." .SmartFormat(aspectType.FormatCSharp(), string.Join(", ", constructorArgs.Select(arg => arg == null ? "[ANY]" : arg.GetType().FormatCSharp())) ); throw new ArgumentException(strErrorMsg, "aspectType"); } } this.InstantiateIfMissing = missingAspectOption; }