Exemplo n.º 1
0
 public static Type MakeGenericType(Type genericTypeDefinition, params Type[] innerTypes)
 {
     ValidationUtils.ArgumentNotNull(genericTypeDefinition, "genericTypeDefinition");
     ValidationUtils.ArgumentNotNullOrEmpty(innerTypes, "innerTypes");
     ValidationUtils.ArgumentConditionTrue(TypeExtensions.IsGenericTypeDefinition(genericTypeDefinition), "genericTypeDefinition", StringUtils.FormatWith("Type {0} is not a generic type definition.", CultureInfo.InvariantCulture, genericTypeDefinition));
     return(genericTypeDefinition.MakeGenericType(innerTypes));
 }
Exemplo n.º 2
0
        public static void GetDictionaryKeyValueTypes(Type dictionaryType, out Type keyType, out Type valueType)
        {
            ValidationUtils.ArgumentNotNull(dictionaryType, "type");
            Type implementingType;

            if (ImplementsGenericDefinition(dictionaryType, typeof(IDictionary <,>), out implementingType))
            {
                if (TypeExtensions.IsGenericTypeDefinition(implementingType))
                {
                    throw new Exception(StringUtils.FormatWith("Type {0} is not a dictionary.", CultureInfo.InvariantCulture, dictionaryType));
                }
                Type[] genericArguments = implementingType.GetGenericArguments();
                keyType   = genericArguments[0];
                valueType = genericArguments[1];
            }
            else
            {
                if (!typeof(IDictionary).IsAssignableFrom(dictionaryType))
                {
                    throw new Exception(StringUtils.FormatWith("Type {0} is not a dictionary.", CultureInfo.InvariantCulture, dictionaryType));
                }
                keyType   = null;
                valueType = null;
            }
        }
Exemplo n.º 3
0
 public static bool ImplementsGenericDefinition(Type type, Type genericInterfaceDefinition, out Type implementingType)
 {
     ValidationUtils.ArgumentNotNull(type, "type");
     ValidationUtils.ArgumentNotNull(genericInterfaceDefinition, "genericInterfaceDefinition");
     if (!TypeExtensions.IsInterface(genericInterfaceDefinition) || !TypeExtensions.IsGenericTypeDefinition(genericInterfaceDefinition))
     {
         throw new ArgumentNullException(StringUtils.FormatWith("'{0}' is not a generic interface definition.", CultureInfo.InvariantCulture, genericInterfaceDefinition));
     }
     if (TypeExtensions.IsInterface(type) && TypeExtensions.IsGenericType(type))
     {
         Type genericTypeDefinition = type.GetGenericTypeDefinition();
         if (genericInterfaceDefinition == genericTypeDefinition)
         {
             implementingType = type;
             return(true);
         }
     }
     foreach (Type type1 in type.GetInterfaces())
     {
         if (TypeExtensions.IsGenericType(type1))
         {
             Type genericTypeDefinition = type1.GetGenericTypeDefinition();
             if (genericInterfaceDefinition == genericTypeDefinition)
             {
                 implementingType = type1;
                 return(true);
             }
         }
     }
     implementingType = null;
     return(false);
 }
Exemplo n.º 4
0
 public static bool InheritsGenericDefinition(Type type, Type genericClassDefinition, out Type implementingType)
 {
     ValidationUtils.ArgumentNotNull(type, "type");
     ValidationUtils.ArgumentNotNull(genericClassDefinition, "genericClassDefinition");
     if (!TypeExtensions.IsClass(genericClassDefinition) || !TypeExtensions.IsGenericTypeDefinition(genericClassDefinition))
     {
         throw new ArgumentNullException(StringUtils.FormatWith("'{0}' is not a generic class definition.", CultureInfo.InvariantCulture, genericClassDefinition));
     }
     return(InheritsGenericDefinitionInternal(type, genericClassDefinition, out implementingType));
 }
Exemplo n.º 5
0
        public static Type GetCollectionItemType(Type type)
        {
            ValidationUtils.ArgumentNotNull(type, "type");
            if (type.IsArray)
            {
                return(type.GetElementType());
            }
            Type implementingType;

            if (ImplementsGenericDefinition(type, typeof(IEnumerable <>), out implementingType))
            {
                if (TypeExtensions.IsGenericTypeDefinition(implementingType))
                {
                    throw new Exception(StringUtils.FormatWith("Type {0} is not a collection.", CultureInfo.InvariantCulture, type));
                }
                return(implementingType.GetGenericArguments()[0]);
            }
            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                return(null);
            }
            throw new Exception(StringUtils.FormatWith("Type {0} is not a collection.", CultureInfo.InvariantCulture, type));
        }
Exemplo n.º 6
0
 public static bool IsInstantiatableType(Type t)
 {
     ValidationUtils.ArgumentNotNull(t, "t");
     return(!TypeExtensions.IsAbstract(t) && !TypeExtensions.IsInterface(t) && (!t.IsArray && !TypeExtensions.IsGenericTypeDefinition(t)) && (!(t == typeof(void)) && HasDefaultConstructor(t)));
 }