示例#1
0
        /// <summary>
        /// Determina o DataType apropriado para representar o tipo ou instância indicado.
        /// </summary>
        /// <param name="typeOrInstance">O tipo ou a instância testada.</param>
        /// <returns>O DataType mais apropriado.</returns>
        public static string FromType(object typeOrInstance)
        {
            if (typeOrInstance == null)
            {
                return(null);
            }

            var type = (typeOrInstance is Type) ? (Type)typeOrInstance : typeOrInstance.GetType();

            type = TypeOf.Var(type) ?? type;
            type = Nullable.GetUnderlyingType(type) ?? type;

            var isList = false;

            if (type.IsArray)
            {
                isList = true;
                type   = type.GetElementType();
            }
            else if (typeof(IList <>).IsAssignableFrom(type))
            {
                isList = true;
                type   = type.GetGenericArguments().Single();
            }

            var typeName = Canonicalize(type);

            if (isList)
            {
                typeName += "[]";
            }

            if (typeName.Contains("AnonymousType"))
            {
                typeName = "AnonymousType";
            }

            return(typeName);
        }