/// <summary> /// Maps a concrete type to its interface. /// </summary> /// <param name="type"> /// The type that is being injected. /// </param> /// <param name="type"> /// The runtime type to be mapped /// </param> /// <returns> /// The interface to which the type is mapped. /// </returns> internal static Type MapType(Type injectedType, Type type) { Boolean covariant = type.Name.Contains("LocalCollection"); Boolean isFactory = injectedType.Name.Contains("Factory"); // Return generic parameters without mapping. if (type.IsGenericParameter) { return(type); } // Return the type name for the type if the type is not part of the injected assmebly. if (type.Assembly != injectedType.Assembly) { return(type); } // Get the interfaces that are defined on the type (not the base types) Type[] baseTypes = GeneratedRuntimeType.EnumerateBaseTypes(type).ToArray(); Type[] baseIface = baseTypes.SelectMany(b => b.GetInterfaces()).ToArray(); Type[] typeIface = type.GetInterfaces().Except(baseIface).ToArray(); // Return the first interface, excluding inferred interfaces. Type[] inference = typeIface.SelectMany(i => i.GetInterfaces()).ToArray(); Type[] exclusion = typeIface.Except(baseIface).ToArray(); return(exclusion.FirstOrDefault() ?? type); }
/// <summary> /// Enumerates all types in the inheritence chain. /// </summary> internal static IEnumerable <Type> EnumerateBaseTypes(Type runtimeType) { if (runtimeType.BaseType != null) { yield return(runtimeType.BaseType); foreach (Type type in GeneratedRuntimeType.EnumerateBaseTypes(runtimeType.BaseType)) { yield return(type); } } }