/// <summary> /// Combines two sets of modifers. /// The first set has priority over the second one. /// This function is cached in order to give only one instance for each possible combination. That allows optimizations in the rest of the library. /// </summary> internal static CustomModifiers GetCombinationOfTwoCustomModifiers( CustomModifiers customParameters1, CustomModifiers customParameters2) { CustomModifiers ret; var k = new Data.ComparableKeyValuePair <CustomModifiers, CustomModifiers>(customParameters1, customParameters2); if (!_CombineTwoCustomParametersCache.TryGetValue(k, out ret)) { ret = _CombineTwoCustomModifiers(customParameters1, customParameters2); _CombineTwoCustomParametersCache.Add(k, ret); } return(ret); }
internal void Init() { // Adds the caller's modifiers to the list. { var callerAssembly = Tools.GetEntryAssembly(); if (callerAssembly != null) { var ma = ModifiersAssembly.GetModifiersAssembly(callerAssembly); if (this.ModifiersAssemblies == null) { this.ModifiersAssemblies = new ModifiersAssembly[1] { ma } } ; else if (!this.ModifiersAssemblies.Contains(ma)) { var mas = new List <ModifiersAssembly>(this.ModifiersAssemblies); mas.Add(ma); this.ModifiersAssemblies = mas.ToArray(); } } } // Adds the internal modifiers to the list. if (this.ModifiersAssemblies == null) { this.ModifiersAssemblies = new ModifiersAssembly[1] { UniversalSerializer.InternalModifiersAssembly } } ; else if (!this.ModifiersAssemblies.Contains(UniversalSerializer.InternalModifiersAssembly)) { var mas = new List <ModifiersAssembly>(this.ModifiersAssemblies); mas.Add(UniversalSerializer.InternalModifiersAssembly); this.ModifiersAssemblies = mas.ToArray(); } { // this.customModifiers is the aggregation of all modifiers of all listed assemblies, more internal. CustomModifiers cm = CustomModifiers.Empty; foreach (var ma in this.ModifiersAssemblies) { cm = cm.GetCombinationWithOtherCustomModifiers(ma.aggregatedCustomModifiers); } this.customModifiers = cm; } }
// - - - ModifiersAssembly(Assembly assembly) { this.assembly = assembly; var cmt = new List <CustomModifiers>(); var types = assembly.GetTypes(); CustomModifiers acm = CustomModifiers.Empty; foreach (Type t in types) { if (TypeEx.IsClass(t)) // optimization. { if (t.Inherits(typeof(CustomModifiers))) { var cm = (CustomModifiers)Activator.CreateInstance(t); if (!(cm is CustomModifiers_Empty)) { acm = acm.GetCombinationWithOtherCustomModifiers(cm); } } } } this.aggregatedCustomModifiers = acm; }
// -------------- /// <summary> /// Combines two sets of modifers. /// The current set has priority over the second one. /// This function is cached in order to give only one instance for each possible combination. That allows optimizations in the rest of the library. /// </summary> internal CustomModifiers GetCombinationWithOtherCustomModifiers(CustomModifiers customParameters2) { return(CustomModifiers.GetCombinationOfTwoCustomModifiers(this, customParameters2)); }
/// <summary> /// Combines two sets of parameters. /// The first set has priority over the second one. /// </summary> static CustomModifiers _CombineTwoCustomModifiers( CustomModifiers customParameters1, CustomModifiers customParameters2) { if (customParameters1 == null || customParameters1 is CustomModifiers_Empty) { return(customParameters2); } //CustomModifiers ret = new CustomModifiers(); ITypeContainer[] Containers; { if ((customParameters1.Containers == null) && (customParameters2.Containers == null)) { Containers = null; } else { if (customParameters1.Containers == null) { Containers = customParameters2.Containers; } else { if (customParameters2.Containers == null) { Containers = customParameters1.Containers; } else { var l = new List <ITypeContainer>(customParameters1.Containers); l.AddRange(customParameters2.Containers); Containers = l.ToArray(); } } } } FilterSet[] FilterSets; { if ((customParameters1.FilterSets == null) && (customParameters2.FilterSets == null)) { FilterSets = null; } else { if (customParameters1.FilterSets == null) { FilterSets = customParameters2.FilterSets; } else { if (customParameters2.FilterSets == null) { FilterSets = customParameters1.FilterSets; } else { var l = new List <FilterSet>(customParameters1.FilterSets); l.AddRange(customParameters2.FilterSets); FilterSets = l.ToArray(); } } } } Type[] ForcedParametricConstructorTypes; { if ((customParameters1.ForcedParametricConstructorTypes == null) && (customParameters2.ForcedParametricConstructorTypes == null)) { ForcedParametricConstructorTypes = null; } else { if (customParameters1.ForcedParametricConstructorTypes == null) { ForcedParametricConstructorTypes = customParameters2.ForcedParametricConstructorTypes; } else { if (customParameters2.ForcedParametricConstructorTypes == null) { ForcedParametricConstructorTypes = customParameters1.ForcedParametricConstructorTypes; } else { var l = new List <Type>(customParameters1.ForcedParametricConstructorTypes); l.AddRange(customParameters2.ForcedParametricConstructorTypes); ForcedParametricConstructorTypes = l.ToArray(); } } } } return(new CustomModifiers(Containers, FilterSets, ForcedParametricConstructorTypes)); }