Ejemplo n.º 1
0
        public static bool IsInterface(this Type type)
        =>
        type.EnsureNotNull(nameof(type)).Value
#if TRG_NETSTANDARD1_5 && !TRG_NETFRAMEWORK
        .GetTypeInfo()
#endif
        .IsInterface;
Ejemplo n.º 2
0
        public static Assembly GetAssembly(this Type type)
        =>
        type.EnsureNotNull(nameof(type)).Value
#if TRG_NETSTANDARD1_5 && !TRG_NETFRAMEWORK
        .GetTypeInfo()
#endif
        .Assembly;
Ejemplo n.º 3
0
        public static bool IsGenericTypeDefinition(this Type type)
        =>
        type.EnsureNotNull(nameof(type)).Value
#if TRG_NETSTANDARD1_5 && !TRG_NETFRAMEWORK
        .GetTypeInfo()
#endif
        .IsGenericTypeDefinition;
Ejemplo n.º 4
0
 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());
         }
     }
 }
Ejemplo n.º 5
0
        public static bool ContainsGenericParameters(this Type type)
        =>
        type.EnsureNotNull(nameof(type)).Value
#if TRG_NETSTANDARD1_5 && !TRG_NETFRAMEWORK
        .GetTypeInfo()
#endif
        .ContainsGenericParameters;
Ejemplo n.º 6
0
        public static Type BaseType(this Type type)
        =>
        type.EnsureNotNull(nameof(type)).Value
#if TRG_NETSTANDARD1_5 && !TRG_NETFRAMEWORK
        .GetTypeInfo()
#endif
        .BaseType;
Ejemplo n.º 7
0
        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));
            }
        }
Ejemplo n.º 8
0
 public static bool IsNullable(this Type type)
 {
     type.EnsureNotNull(nameof(type));
     //
     return(type.IsGenericType() && (type.IsGenericTypeDefinition() ? type : type.GetGenericTypeDefinition()) == __NullableTypeDefinition);
 }
Ejemplo n.º 9
0
 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();
Ejemplo n.º 10
0
 public static bool IsDefined(this Type type, Type attributeType, bool inherit)
 => type.EnsureNotNull(nameof(type)).Value.GetTypeInfo().IsDefined(attributeType.EnsureNotNull(nameof(attributeType)), inherit);
Ejemplo n.º 11
0
 public static bool IsSubclassOf(this Type type, Type ofType)
 => type.EnsureNotNull(nameof(type)).Value.GetTypeInfo().IsSubclassOf(ofType.EnsureNotNull(nameof(ofType)));
Ejemplo n.º 12
0
 public static Type GetGenericTypeDefinition(this Type type)
 =>
 type.EnsureNotNull(nameof(type)).Value.GetTypeInfo().GetGenericTypeDefinition();
Ejemplo n.º 13
0
        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());
        }
Ejemplo n.º 14
0
        // 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);
        }