Exemplo n.º 1
0
        private static TypeUsageInfo CreateTypeUsageInfo(Type type, TypeUsageInfo[] typeArguments = null, string shortName = null)
        {
            var name = type.Name;

            if (type.IsGenericTypeDefinition)
            {
                var index = name.IndexOf('`');
                if (index != -1)
                {
                    name = name.Substring(0, index);
                }
            }
            var typeNamespace = type.Namespace;

            if (type.IsEnum)
            {
                return(TypeUsageInfo.CreateEnum(name, typeNamespace));
            }

            if (type.IsGenericTypeDefinition && (typeArguments == null ||
                                                 type.GetGenericArguments().Length != typeArguments.Length))
            {
                throw new ArgumentOutOfRangeException("typeArguments");
            }

            if (type.IsClass)
            {
                return(TypeUsageInfo.Create(name, typeNamespace, TypeUsageInfoConfiguration.Class, shortName, typeArguments));
            }
            if (type.IsInterface)
            {
                return(TypeUsageInfo.Create(name, typeNamespace, TypeUsageInfoConfiguration.Interface, shortName, typeArguments));
            }
            if (type.IsValueType)
            {
                return(TypeUsageInfo.Create(name, typeNamespace, TypeUsageInfoConfiguration.ValueType, shortName, typeArguments));
            }
            throw new NotSupportedException(string.Format("Can't convert type '{0}' to TypeUsageInfo", type.FullName));
        }