private static string ReplacePlaceHolder( Builder builder, string targetInfoArgument, string targetInfoPropertyOrMethodName, string targetInfoReturnTypeName, BuilderCustomAttribute builderCustomAttribute, bool throwError) { if (string.IsNullOrEmpty(targetInfoArgument)) { return(null); } var result = Regex.Replace(targetInfoArgument, @"{CtorArgument:(.+?)}", x => { var constructorPlaceholderSplit = x.Value.Split(':'); var definedIndexOrName = constructorPlaceholderSplit.Length > 1 ? constructorPlaceholderSplit[1].Substring(0, constructorPlaceholderSplit[1].Length - 1) : ""; if (string.IsNullOrEmpty(definedIndexOrName)) { builder.Log(throwError ? LogTypes.Error : LogTypes.Info, $"No name or index defined for '{builderCustomAttribute.Fullname}'."); return(""); } if (uint.TryParse(definedIndexOrName, out uint index)) { if (index >= builderCustomAttribute.ConstructorArguments.Length) { builder.Log(throwError ? LogTypes.Error : LogTypes.Info, $"The given constructor for '{builderCustomAttribute.Fullname}' does not have an argument index {index}."); return(""); } else { return(builderCustomAttribute.ConstructorArguments[index].Value as string); } } var argument = builderCustomAttribute.GetConstructorArgument(definedIndexOrName).Value as string; if (argument == null) { builder.Log(throwError ? LogTypes.Error : LogTypes.Info, $"The given constructor for '{builderCustomAttribute.Fullname}' does not have an argument named {definedIndexOrName}."); } else { return(argument); } return(""); }); return(result .Replace("{Name}", targetInfoPropertyOrMethodName.EnclosedIn().UpperCaseFirstLetter()) .Replace("{ReturnType}", targetInfoReturnTypeName ?? "void")); }
public static bool GetIsSupressed(InterceptorInfo interceptorInfo, BuilderType declaringType, BuilderCustomAttributeCollection appliedCustomAttributes, BuilderCustomAttribute thisAttribute, string methodOrPropertyName, bool isMethod) { if (interceptorInfo.HasSuppressRule) { var attributes = appliedCustomAttributes.Select(x => x.Fullname); var result = interceptorInfo.SuppressRuleAttributeTypes.Any(x => attributes.Contains(x.FullName)); if (result) { declaringType.Log(LogTypes.Info, $"Suppressed - The { (isMethod ? "method" : "property") } '{methodOrPropertyName}' is not intercepted by '{thisAttribute.Type.Name}' because of a rule."); thisAttribute.Remove(); return(true); } } if (interceptorInfo.HasInterfaceOrBaseClassRequirement) { var interfacesAndBaseClasses = declaringType.Interfaces.Concat(declaringType.BaseClasses).Select(x => x.Fullname); var hasRequired = interceptorInfo.InterfaceOrBaseClassRequirement.Any(x => x.Item2 == 1); var requiredInterfacesAndBaseClasses = interceptorInfo.InterfaceOrBaseClassRequirement.Any(x => x.Item2 == 1 && interfacesAndBaseClasses.Any(y => y.StartsWith(x.Item1.FullName))); var optionalInterfacesAnsBaseClasses = interceptorInfo.InterfaceOrBaseClassRequirement.Any(x => x.Item2 == 0 && interfacesAndBaseClasses.Any(y => y.StartsWith(x.Item1.FullName))); if (hasRequired && !requiredInterfacesAndBaseClasses) { thisAttribute.Remove(); return(true); } if (!optionalInterfacesAnsBaseClasses) { thisAttribute.Remove(); return(true); } } return(false); }
public InjectAttributeValues(BuilderCustomAttribute builderCustomAttribute) { if (builderCustomAttribute.ConstructorArguments != null && builderCustomAttribute.ConstructorArguments.Length > 0) { this.Arguments = builderCustomAttribute.ConstructorArguments[0].Value as CustomAttributeArgument[]; } if (builderCustomAttribute.Properties.ContainsKey("ContractType")) { this.ContractType = (builderCustomAttribute.Properties["ContractType"].Value as TypeReference)?.ToBuilderType(); } if (builderCustomAttribute.Properties.ContainsKey("ContractName")) { this.ContractName = builderCustomAttribute.Properties["ContractName"].Value as string; } if (builderCustomAttribute.Properties.ContainsKey("InjectFirst")) { this.InjectFirst = (bool)builderCustomAttribute.Properties["InjectFirst"].Value; } if (builderCustomAttribute.Properties.ContainsKey("IsOrdered")) { this.IsOrdered = (bool)builderCustomAttribute.Properties["IsOrdered"].Value; } if (builderCustomAttribute.Properties.ContainsKey("MakeThreadSafe")) { this.MakeThreadSafe = (bool)builderCustomAttribute.Properties["MakeThreadSafe"].Value; } if (builderCustomAttribute.Properties.ContainsKey("ForceDontCreateMany")) { this.ForceDontCreateMany = (bool)builderCustomAttribute.Properties["ForceDontCreateMany"].Value; } if (builderCustomAttribute.Properties.ContainsKey("NoPreloading")) { this.NoPreloading = (bool)builderCustomAttribute.Properties["NoPreloading"].Value; } }
private static AssignMethodAttributeInfo[] GetAllAssignMethodAttributedFields(Method attributedMethod, BuilderCustomAttribute builderCustomAttribute, BuilderType targetType, string name, string returnTypeName) { var fields = builderCustomAttribute.Type .GetAttributedFields() .Where(x => x.Field.IsPublic && !x.Field.IsStatic && x.Attribute.Fullname == __AssignMethodAttribute.Type.Fullname); return(fields .Select(x => { var throwError = !(bool)x.Attribute.ConstructorArguments[1].Value; return new AssignMethodAttributeInfo { AttributeField = x.Field, TargetMethodName = ReplacePlaceHolder(targetType.Builder, (x.Attribute.ConstructorArguments[0].Value as string ?? "", name, returnTypeName), builderCustomAttribute, throwError), TargetMethodReturnType = GetDelegateType(x.Field.FieldType), ThrowError = throwError, Type = targetType, ParameterTypes = GetParameters(x.Field.FieldType), AttributedMethod = attributedMethod }; }).ToArray()); }
public static void Implement(Builder builder) { builder.Log(LogTypes.Info, "Creating Cauldron Cache"); var cauldron = builder.GetType("CauldronInterceptionHelper", SearchContext.Module); var componentAttribute = BuilderTypes.ComponentAttribute; var genericComponentAttribute = BuilderTypes.GenericComponentAttribute; // Before we start let us find all factoryextensions and add a component attribute to them var factoryResolverInterface = (BuilderType)BuilderTypes.IFactoryExtension; AddComponentAttribute(builder, builder.FindTypesByInterface(factoryResolverInterface), x => factoryResolverInterface); // Get all Components var componentTypes = new List <BuilderType>(); var components = builder .FindTypesByAttribute(componentAttribute) .Concat(builder .CustomAttributes .Where(x => x.Type == genericComponentAttribute) .Select(x => new { Attribute = BuilderCustomAttribute.Create(componentAttribute, x.ConstructorArguments.Skip(1)), Type = (x.ConstructorArguments[0].Value as TypeReference).ToBuilderType() }) .Where(x => { if (x.Type.IsAbstract) { builder.Log(LogTypes.Error, $"The GenericComponentAttribute does not support abstract types."); return(false); } if (x.Type.IsInterface) { builder.Log(LogTypes.Error, $"The GenericComponentAttribute does not support interfaces."); return(false); } if (x.Type.IsValueType) { builder.Log(LogTypes.Error, $"The GenericComponentAttribute does not support value types."); return(false); } return(true); }) .Select(x => new AttributedType(x.Type, x.Attribute))); foreach (var component in components) { var factoryType = FactoryTypeInfoWeaver.Create(component); componentTypes.Add(factoryType); } builder.Log(LogTypes.Info, "Adding component IFactoryCache Interface"); var linqEnumerable = builder.GetType("System.Linq.Enumerable", SearchContext.AllReferencedModules); var ifactoryTypeInfo = BuilderTypes.IFactoryTypeInfo.BuilderType.Import().MakeArray(); var getComponentsFromOtherAssemblies = builder.ReferencedAssemblies .Select(x => x.MainModule.GetType("CauldronInterceptionHelper")) .Where(x => x != null) .Select(x => x.ToBuilderType()) .Select(x => x.GetMethod("GetComponents", false)?.Import()) .Where(x => x != null) .ToArray(); var getComponentsMethod = cauldron.CreateMethod(Modifiers.Public | Modifiers.Static, ifactoryTypeInfo, "GetComponents"); var componentsField = cauldron.CreateField(Modifiers.PrivateStatic, ifactoryTypeInfo, "<FactoryType>f__components"); getComponentsMethod .NewCoder() .NewCoder() .Context(context => { context.If(x => x.Load(componentsField).IsNotNull(), then => then.Load(componentsField).Return()); var resultValue = context.GetOrCreateReturnVariable(); context.SetValue(resultValue, x => x.Newarr(BuilderTypes.IFactoryTypeInfo, componentTypes.Count)); for (int i = 0; i < componentTypes.Count; i++) { context .Load(resultValue) .StoreElement(context.NewCoder().NewObj(componentTypes[i].ParameterlessContructor), i); } context.SetValue(componentsField, resultValue); return(context.Load(resultValue).Return()); }) .Replace(); var voidMain = builder.GetMain(); if (voidMain != null) { voidMain.NewCoder() .Context(context => { context.Call(builder.GetType("Cauldron.Activator.FactoryCore").GetMethod("SetComponents", 1, true), x => x.Call(getComponentsMethod)); if (getComponentsFromOtherAssemblies.Length > 0) { foreach (var item in getComponentsFromOtherAssemblies) { context.Call(builder.GetType("Cauldron.Activator.FactoryCore").GetMethod("SetComponents", 1, true), x => x.Call(item)); } } return(context); }) .End .Insert(InsertionPosition.Beginning); } var injectAttribute = new BuilderType[] { builder.GetType("Cauldron.Activator.InjectAttribute") }; var properties = builder.FindPropertiesByAttributes(injectAttribute).ToArray(); var fields = builder.FindFieldsByAttributes(injectAttribute).ToArray(); ImplementInjectField(builder, fields); ImplementInjectProperties(builder, properties); ReplaceFactoryCreate(builder); }