예제 #1
0
        /// <summary>
        /// Resolves the supplied generic <paramref name="typeName"/> to a
        /// <see cref="System.Type"/> instance.
        /// </summary>
        /// <param name="typeName">
        /// The unresolved (possibly generic) name of a <see cref="System.Type"/>.
        /// </param>
        /// <returns>
        /// A resolved <see cref="System.Type"/> instance.
        /// </returns>
        /// <exception cref="System.TypeLoadException">
        /// If the supplied <paramref name="typeName"/> could not be resolved
        /// to a <see cref="System.Type"/>.
        /// </exception>
        public override Type Resolve(string typeName)
        {
            if (string.IsNullOrWhiteSpace(typeName))
            {
                throw BuildTypeLoadException(typeName);
            }

            Type type        = null;
            var  genericInfo = new GenericArgumentsHolder(typeName);

            try
            {
                if (genericInfo.ContainsGenericArguments)
                {
                    type = TypeResolutionUtil.ResolveType(genericInfo.GenericTypeName);

                    if (!genericInfo.IsGenericDefinition)
                    {
                        var unresolvedGenericArgs = genericInfo.GetGenericArguments();
                        var genericArgs           = new Type[unresolvedGenericArgs.Length];
                        for (int i = 0; i < unresolvedGenericArgs.Length; i++)
                        {
                            genericArgs[i] = TypeResolutionUtil.ResolveType(unresolvedGenericArgs[i]);
                        }

                        type = type.MakeGenericType(genericArgs);
                    }

                    if (genericInfo.IsArrayDeclaration)
                    {
                        typeName = string.Format("{0}{1},{2}", type.FullName, genericInfo.GetArrayDeclaration(), type.GetTypeInfo().Assembly.FullName);
                        type     = null;
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is TypeLoadException)
                {
                    throw;
                }

                throw BuildTypeLoadException(typeName, ex);
            }

            if (type == null)
            {
                type = base.Resolve(typeName);
            }

            return(type);
        }
예제 #2
0
        /// <summary>
        /// Registers an alias for the specified <see cref="System.Type"/>.
        /// </summary>
        /// <remarks>
        /// <p>
        /// This overload does eager resolution of the <see cref="System.Type"/>
        /// referred to by the <paramref name="typeName"/> parameter. It will throw a
        /// <see cref="System.TypeLoadException"/> if the <see cref="System.Type"/> referred
        /// to by the <paramref name="typeName"/> parameter cannot be resolved.
        /// </p>
        /// </remarks>
        /// <param name="alias">
        /// A string that will be used as an alias for the specified
        /// <see cref="System.Type"/>.
        /// </param>
        /// <param name="typeName">
        /// The (possibly partially assembly qualified) name of the
        /// <see cref="System.Type"/> to register the alias for.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        /// If either of the supplied parameters is <see langword="null"/> or
        /// contains only whitespace character(s).
        /// </exception>
        /// <exception cref="System.TypeLoadException">
        /// If the <see cref="System.Type"/> referred to by the supplied
        /// <paramref name="typeName"/> cannot be loaded.
        /// </exception>
        public static void RegisterType(string alias, string typeName)
        {
            AssertUtil.ArgumentNotEmpty(alias, "alias");
            AssertUtil.ArgumentNotEmpty(typeName, "typeName");

            var type = TypeResolutionUtil.ResolveType(typeName);

            if (type.GetTypeInfo().IsGenericTypeDefinition)
            {
                alias += ("`" + type.GetGenericArguments().Length);
            }

            RegisterType(alias, type);
        }