/// <summary> /// Get the default (automatically discovered) configuration for a certain format. /// </summary> /// <param name="format">The format.</param> /// <returns>The default (auto discovered) configuration.</returns> public Configuration GetDefaultConfig(IFormat format) { Dictionary <Type, IPrimitiveSerializer> primitiveConfig = new Dictionary <Type, IPrimitiveSerializer>(); if (PrimitiveSerializers.ContainsKey(format.SerialDataType)) { foreach (IPrimitiveSerializer f in PrimitiveSerializers[format.SerialDataType]) { if (!primitiveConfig.ContainsKey(f.SourceType)) { primitiveConfig.Add(f.SourceType, (IPrimitiveSerializer)Activator.CreateInstance(f.GetType())); } } } else { Logger.Warn(String.Format( "No primitive serializers found for format {0} with serial data type {1}", format.GetType().AssemblyQualifiedName, format.SerialDataType.AssemblyQualifiedName)); } return(new Configuration( format, primitiveConfig.Values, CompositeSerializers.Where((d) => d.Priority > 0).Select(d => (ICompositeSerializer)Activator.CreateInstance(d.GetType())))); }
/// <summary> /// Discovers serializers from an assembly. /// </summary> /// <param name="a">An Assembly.</param> protected void DiscoverFrom(Assembly a) { try { foreach (Type t in a.GetTypes()) { if (t.GetInterface(typeof(IPrimitiveSerializer).FullName) != null && !t.IsAbstract && t.GetConstructor(Type.EmptyTypes) != null && !t.ContainsGenericParameters) { IPrimitiveSerializer primitiveSerializer = (IPrimitiveSerializer)Activator.CreateInstance(t, true); if (!PrimitiveSerializers.ContainsKey(primitiveSerializer.SerialDataType)) { PrimitiveSerializers.Add(primitiveSerializer.SerialDataType, new List <IPrimitiveSerializer>()); } PrimitiveSerializers[primitiveSerializer.SerialDataType].Add(primitiveSerializer); } if (t.GetInterface(typeof(ICompositeSerializer).FullName) != null && !t.IsAbstract && t.GetConstructor(Type.EmptyTypes) != null && !t.ContainsGenericParameters) { CompositeSerializers.Add((ICompositeSerializer)Activator.CreateInstance(t, true)); } if (t.GetInterface(typeof(IFormat).FullName) != null && !t.IsAbstract && t.GetConstructor(Type.EmptyTypes) != null && !t.ContainsGenericParameters) { IFormat format = (IFormat)Activator.CreateInstance(t, true); Formats.Add(format); } } } catch (ReflectionTypeLoadException e) { Logger.Warn("could not analyse assembly: " + a.FullName, e); } }
/// <summary> /// Rediscover available serializers and discard all custom configurations. /// </summary> public void Reset() { customConfigurations.Clear(); PrimitiveSerializers.Clear(); CompositeSerializers.Clear(); Assembly defaultAssembly = Assembly.GetExecutingAssembly(); DiscoverFrom(defaultAssembly); try { foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) { if (a != defaultAssembly) { DiscoverFrom(a); } } } catch (AppDomainUnloadedException x) { Logger.Warn("could not get list of assemblies, AppDomain has already been unloaded", x); } SortCompositeSerializers(); }
/// <summary> /// Sorts the composite serializers according to their priority. /// </summary> protected void SortCompositeSerializers() { CompositeSerializers.Sort(new PriortiySorter()); }