GetType() public static method

Gets the specified type from the loaded assemblies.
The is null or whitespace.
public static GetType ( string typeNameWithAssembly, bool ignoreCase = false ) : Type
typeNameWithAssembly string The name of the type including namespace and assembly, formatted with the method.
ignoreCase bool A value indicating whether the case should be ignored.
return System.Type
Example #1
0
        /// <summary>
        /// Gets the type by splitting internal types. This means that System.Collections.List`1[[MyCustomType.Item]] will be splitted
        /// and resolved separately.
        /// </summary>
        /// <param name="typeWithInnerTypes">The type with inner types.</param>
        /// <returns></returns>
        private static Type GetTypeBySplittingInternals(string typeWithInnerTypes)
        {
            // Try fast method first
            var fastType = Type.GetType(typeWithInnerTypes);

            if (fastType != null)
            {
                return(fastType);
            }

            if (typeWithInnerTypes.EndsWith("[]"))
            {
                // Array type
                var arrayTypeElementString = typeWithInnerTypes.Replace("[]", string.Empty);
                var arrayTypeElement       = TypeCache.GetType(arrayTypeElementString);
                if (arrayTypeElement != null)
                {
                    return(arrayTypeElement.MakeArrayType());
                }

                return(null);
            }

            var innerTypes           = new List <Type>();
            var innerTypesShortNames = TypeHelper.GetInnerTypes(typeWithInnerTypes);

            if (innerTypesShortNames.Length > 0)
            {
                foreach (var innerTypesShortName in innerTypesShortNames)
                {
                    var innerType = TypeCache.GetType(innerTypesShortName);
                    if (innerType == null)
                    {
                        return(null);
                    }

                    innerTypes.Add(innerType);
                }

                var innerTypesNames = new List <string>();
                foreach (var innerType in innerTypes)
                {
                    innerTypesNames.Add(innerType.AssemblyQualifiedName);
                }

                var firstBracketIndex          = typeWithInnerTypes.IndexOf('[');
                var typeWithImprovedInnerTypes = string.Format("{0}[{1}]", typeWithInnerTypes.Substring(0, firstBracketIndex), TypeHelper.FormatInnerTypes(innerTypesNames.ToArray()));

                var fallbackType = Type.GetType(typeWithImprovedInnerTypes);
                return(fallbackType);
            }

            // This is not yet supported or type is really not available
            return(null);
        }
Example #2
0
        /// <summary>
        /// Gets the type by splitting internal types. This means that System.Collections.List`1[[MyCustomType.Item]] will be splitted
        /// and resolved separately.
        /// </summary>
        /// <param name="typeWithInnerTypes">The type with inner types.</param>
        /// <returns></returns>
        private static Type GetTypeBySplittingInternals(string typeWithInnerTypes)
        {
            // Try fast method first
            var fastType = Type.GetType(typeWithInnerTypes);

            if (fastType != null)
            {
                return(fastType);
            }

            if (typeWithInnerTypes.EndsWith("[]"))
            {
                // Array type
                var arrayTypeElementString = typeWithInnerTypes.Replace("[]", string.Empty);
                var arrayTypeElement       = GetType(arrayTypeElementString, allowInitialization: false);
                if (arrayTypeElement != null)
                {
                    return(arrayTypeElement.MakeArrayType());
                }

                return(null);
            }

            var firstBracketIndex = typeWithInnerTypes.IndexOf('[');

            if (firstBracketIndex < 0)
            {
                // Not a generic type, and we failed to retrieve it the first time
                return(null);
            }

            var genericTypeName = typeWithInnerTypes.Substring(0, firstBracketIndex);
            var genericType     = TypeCache.GetType(genericTypeName);

            if (genericType is null)
            {
                // We couldn't resolve List`1
                return(null);
            }

            var innerTypesShortNames = TypeHelper.GetInnerTypes(typeWithInnerTypes);
            var innerTypeCount       = innerTypesShortNames.Length;

            if (innerTypeCount > 0)
            {
                var innerTypes = new Type[innerTypeCount];

                for (var i = 0; i < innerTypeCount; i++)
                {
                    var innerType = GetType(innerTypesShortNames[i], allowInitialization: false);
                    if (innerType is null)
                    {
                        return(null);
                    }

                    innerTypes[i] = innerType;
                }

                var finalType = genericType.MakeGenericTypeEx(innerTypes);
                return(finalType);
            }

            // This is not yet supported or type is really not available
            return(null);
        }