public virtual void FillFrom(INamedInstanceRegistry <T> registry, Assembly assembly) { var pairs = from type in assembly.ExceptionSafeGetTypes() where type.GetTypeInfo().IsPublic where !type.GetTypeInfo().IsAbstract where typeof(T).IsAssignableFrom(type) let name = FindName(type) where !string.IsNullOrEmpty(name) select new { Name = name, Type = type }; foreach (var pair in pairs) { try { if (pair.Type.ContainsGenericParameters) { continue; } var converter = Activator.CreateInstance(pair.Type) as T; Log.Trace("Registering value converter {0}:{1}", pair.Name, pair.Type.Name); registry.AddOrOverwrite(pair.Name, converter); } catch (Exception) { // ignore this } } }
public virtual void FillFrom(INamedInstanceRegistry <T> registry, Type type) { if (type.GetTypeInfo().IsAbstract) { FillFromStatic(registry, type); } else { FillFromInstance(registry, type); } }
protected virtual void FillFromStatic(INamedInstanceRegistry <T> registry, Type type) { var pairs = from field in type.GetFields() where field.IsStatic where field.IsPublic where typeof(T).IsAssignableFrom(field.FieldType) let converter = field.GetValue(null) as T where converter != null select new { field.Name, Converter = converter }; foreach (var pair in pairs) { registry.AddOrOverwrite(pair.Name, pair.Converter); } }