public static bool IsInterface(this Type type) => type.EnsureNotNull(nameof(type)).Value #if TRG_NETSTANDARD1_5 && !TRG_NETFRAMEWORK .GetTypeInfo() #endif .IsInterface;
public static Assembly GetAssembly(this Type type) => type.EnsureNotNull(nameof(type)).Value #if TRG_NETSTANDARD1_5 && !TRG_NETFRAMEWORK .GetTypeInfo() #endif .Assembly;
public static bool IsGenericTypeDefinition(this Type type) => type.EnsureNotNull(nameof(type)).Value #if TRG_NETSTANDARD1_5 && !TRG_NETFRAMEWORK .GetTypeInfo() #endif .IsGenericTypeDefinition;
public static IEnumerable <Type> GetHierarchyOfGenericDefinitions(this Type type, bool includeSelf = true) { type.EnsureNotNull(nameof(type)); // if (includeSelf && type.IsGenericType()) { yield return(type.IsGenericTypeDefinition() ? type : type.GetGenericTypeDefinition()); } // Return as for non-interface type. // if (!type.IsInterface()) { var currentType = type.BaseType(); for (; currentType != null;) { if (currentType.IsGenericType()) { yield return(currentType.GetGenericTypeDefinition()); } currentType = currentType.BaseType(); } } // Return as for interface type. // foreach (var interfaceType in type.GetInterfaces()) { if (interfaceType.IsGenericType()) { yield return(interfaceType.GetGenericTypeDefinition()); } } }
public static bool ContainsGenericParameters(this Type type) => type.EnsureNotNull(nameof(type)).Value #if TRG_NETSTANDARD1_5 && !TRG_NETFRAMEWORK .GetTypeInfo() #endif .ContainsGenericParameters;
public static Type BaseType(this Type type) => type.EnsureNotNull(nameof(type)).Value #if TRG_NETSTANDARD1_5 && !TRG_NETFRAMEWORK .GetTypeInfo() #endif .BaseType;
public static IEnumerable <Type> GetHierarchyOfClass(this Type type, Type to) { type.EnsureNotNull(nameof(type)).EnsureClass(); to.EnsureNotNull(nameof(to)).EnsureClass(); // if (type.IsSubclassOf(to)) { while (true) { yield return(type); if (type == to) { break; } type = type.BaseType(); } } else if (type == to) { yield return(to); } else { throw new ArgumentException(FormatXResource(typeof(ArgumentException), "ValueIsInvalid/Type/InvalidDerivation", type, to), nameof(type)); } }
public static bool IsNullable(this Type type) { type.EnsureNotNull(nameof(type)); // return(type.IsGenericType() && (type.IsGenericTypeDefinition() ? type : type.GetGenericTypeDefinition()) == __NullableTypeDefinition); }
public static Attribute[] GetCustomAttributes(this Type type, Type attributeType, bool inherit) => type.EnsureNotNull(nameof(type)).Value.GetTypeInfo().GetCustomAttributes(attributeType.EnsureNotNull(nameof(attributeType)), inherit).Cast <Attribute>().ToArray();
public static bool IsDefined(this Type type, Type attributeType, bool inherit) => type.EnsureNotNull(nameof(type)).Value.GetTypeInfo().IsDefined(attributeType.EnsureNotNull(nameof(attributeType)), inherit);
public static bool IsSubclassOf(this Type type, Type ofType) => type.EnsureNotNull(nameof(type)).Value.GetTypeInfo().IsSubclassOf(ofType.EnsureNotNull(nameof(ofType)));
public static Type GetGenericTypeDefinition(this Type type) => type.EnsureNotNull(nameof(type)).Value.GetTypeInfo().GetGenericTypeDefinition();
public static Type SelectMostCompatibleType(this Type source, params Type[] candidates) { const int maxInheritanceMarkInclusive = 2048; source.EnsureNotNull(nameof(source)); if (!source.IsClass()) { throw new ArgumentException(FormatXResource(typeof(ArgumentException), "ValueIsInvalid/Type/NotClass", source), nameof(source)); } if (source.ContainsGenericParameters()) { throw new ArgumentException(FormatXResource(typeof(ArgumentException), "ValueIsInvalid/Type/ContainsGenericParameters", source), nameof(source)); } if (candidates == null) { throw new ArgumentNullException("candidateTypes"); } if (candidates.Length < 1) { return(null); } if (candidates.Length == 1) { var candidateType = candidates[0]; if (candidateType == null) { throw new ArgumentException(FormatXResource(typeof(Array), "CanNotContainNull/NullAt", 0), "candidateTypes"); } return(candidateType.IsAssignableFrom(source) || (candidateType.IsGenericTypeDefinition() && source.GetHierarchyOfGenericDefinitions().Any(y => y == candidateType)) ? candidateType : null); } var inheritanceMarks = new Dictionary <Type, int>(); var inheritanceMark = -1; var sourceTypeBaseType = source; inheritanceMarks.Add(source, ++inheritanceMark); if (source.IsGenericType()) { inheritanceMarks.Add(source.GetGenericTypeDefinition(), inheritanceMark); } for (; (sourceTypeBaseType = sourceTypeBaseType.BaseType()) != null;) { if (inheritanceMark == maxInheritanceMarkInclusive) { throw new EonException(string.Format("It is impossible to select the most compatible type for the '{0}' type from the specified set of types.", source)); } inheritanceMarks.Add(sourceTypeBaseType, ++inheritanceMark); if (sourceTypeBaseType.IsGenericType()) { inheritanceMarks.Add(sourceTypeBaseType.GetGenericTypeDefinition(), inheritanceMark); } } foreach (var sourceTypeInterface in source.GetInterfaces()) { if (inheritanceMarks.ContainsKey(sourceTypeInterface)) { continue; } foreach (var sourceTypeInterfaceH in sourceTypeInterface.GetHierarchyOfInterface()) { if (inheritanceMark == maxInheritanceMarkInclusive) { throw new EonException($"It is impossible to select the most compatible type for the '{source}' type from the specified set of types."); } if (inheritanceMarks.ContainsKey(sourceTypeInterfaceH)) { inheritanceMarks[sourceTypeInterfaceH] = ++inheritanceMark; } else { inheritanceMarks.Add(sourceTypeInterfaceH, ++inheritanceMark); if (sourceTypeInterfaceH.IsGenericType() && !inheritanceMarks.ContainsKey(sourceTypeInterfaceH.GetGenericTypeDefinition())) { inheritanceMarks.Add(sourceTypeInterfaceH.GetGenericTypeDefinition(), inheritanceMark); } } } } return((from candidateType in candidates.AsEnumerable().Arg(nameof(candidates)).EnsureNoNullElements().Value where inheritanceMarks.ContainsKey(candidateType) && (candidateType.IsAssignableFrom(source) || (candidateType.IsGenericTypeDefinition() && source.GetHierarchyOfGenericDefinitions().Any(y => y == candidateType))) orderby inheritanceMarks[candidateType] ascending select candidateType).FirstOrDefault()); }
// TODO: Put exception messages into the resources. // public static int GetInheritanceLevel(this Type type, Type @base) { type.EnsureNotNull(nameof(type)); @base.EnsureNotNull(nameof(@base)); if (!(type.IsClass() || type.IsInterface())) { throw new ArgumentException(message: FormatXResource(typeof(ArgumentException), "ValueIsInvalid/Type/NotClass", type), paramName: nameof(type)); } else if (!(@base.IsClass() || @base.IsInterface())) { throw new ArgumentException(message: FormatXResource(typeof(ArgumentException), "ValueIsInvalid/Type/NotClass", @base), paramName: nameof(@base)); } else if ([email protected](type)) { throw new ArgumentException(message: FormatXResource(typeof(ArgumentException), "ValueIsInvalid/Type/InvalidDerivation", type, @base), paramName: nameof(type)); } // Func <Type, Type, int> computeInheritanceLevelFunc = (b, d) => { var c = 0; for (c = 0; b != d;) { c++; d = d.BaseType(); } return(c); }; int level; if (@base.IsInterface()) { if (type.IsInterface()) { level = computeInheritanceLevelFunc(@base, type); } else { level = 0; #if TRG_NETFRAMEWORK InterfaceMapping currentInterfaceMapping; for (; derivedClassOrInterface != null;) { currentInterfaceMapping = derivedClassOrInterface.GetInterfaceMap(baseClassOrInterface); if (currentInterfaceMapping.TargetMethods != null && currentInterfaceMapping.TargetMethods.Length > 0) { break; } level++; derivedClassOrInterface = derivedClassOrInterface.BaseType(); } #endif } } else if (type.IsInterface()) { throw new ArgumentException(message: FormatXResource(typeof(ArgumentException), "ValueIsInvalid"), paramName: nameof(type)); } else { level = computeInheritanceLevelFunc(@base, type); } return(level); }