NameOfType() static private method

static private NameOfType ( Type t ) : string
t System.Type
return string
Example #1
0
        internal static string FormatObject(object value)
        {
            if (value == null)
            {
                return("null");
            }
            if (value is string)
            {
                return("\"" + value + "\"");
            }
            if (value is char)
            {
                return("'" + value + "'");
            }

            var exception = value as Exception;

            if (exception != null)
            {
                return("{" + exception.GetType().Name + "}");
            }

            var type = value.GetType();

            if (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == typeof(KeyValuePair <,>))
            {
                var k = type.GetRuntimeProperty("Key").GetValue(value, null);
                var v = type.GetRuntimeProperty("Value").GetValue(value, null);
                return(String.Format("{{{0}:{1}}}", FormatObject(k), FormatObject(v)));
            }
            if (type.GetTypeInfo().ImplementedInterfaces
                .Where(i => i.IsConstructedGenericType)
                .Any(i => i.GetGenericTypeDefinition() == typeof(IGrouping <,>)))
            {
                var k = type.GetRuntimeProperty("Key").GetValue(value, null);
                return(String.Format("{{{0}:{1}}}", FormatObject(k), FormatEnumerable(value)));
            }
            if (value is Type)
            {
                return("typeof(" + ExpressionParser.NameOfType((Type)value) + ")");
            }
            if (value is Delegate)
            {
                var del = (Delegate)value;


                return(String.Format("delegate {0}, type: {2} ({1})", ExpressionParser.NameOfType(del.GetType()), String.Join(", ", del.GetMethodInfo().GetParameters().Select(x => ExpressionParser.NameOfType(x.ParameterType))), ExpressionParser.NameOfType(del.GetMethodInfo().ReturnType)));
            }
            if (value is IEnumerable)
            {
                return(FormatEnumerable(value));
            }
            return(value.ToString());
        }
        internal static string FormatObject(object value)
        {
            if (value == null)
            {
                return("null");
            }
            if (value is string)
            {
                return("\"" + value + "\"");
            }
            if (value is char)
            {
                return("'" + value + "'");
            }

            var exception = value as Exception;

            if (exception != null)
            {
                return("{" + exception.GetType().Name + "}");
            }

            var type = value.GetType();

#if NETCOREAPP1_1
            var isGenericType = new Func <Type, bool>(t => t.GetTypeInfo().IsGenericType);
#else
            var isGenericType = new Func <Type, bool>(t => t.IsGenericType);
#endif
            if (isGenericType(type) && type.GetGenericTypeDefinition() == typeof(KeyValuePair <,>))
            {
                var k = type.GetProperty("Key").GetValue(value, null);
                var v = type.GetProperty("Value").GetValue(value, null);
                return(String.Format("{{{0}:{1}}}", FormatObject(k), FormatObject(v)));
            }
            if (type.GetInterfaces()
                .Where(isGenericType)
                .Any(i => i.GetGenericTypeDefinition() == typeof(IGrouping <,>)))
            {
                var k = type.GetProperty("Key").GetValue(value, null);
                return(String.Format("{{{0}:{1}}}", FormatObject(k), FormatEnumerable(value)));
            }
            if (value is Type)
            {
                return("typeof(" + ExpressionParser.NameOfType((Type)value) + ")");
            }
            if (value is Delegate)
            {
                var del = (Delegate)value;

#if NETCOREAPP1_1
                var method = RuntimeReflectionExtensions.GetMethodInfo(del);
#else
                var method = del.Method;
#endif

                return(String.Format("delegate {0}, type: {2} ({1})", ExpressionParser.NameOfType(del.GetType()), String.Join(", ", method.GetParameters().Select(x => ExpressionParser.NameOfType(x.ParameterType))), ExpressionParser.NameOfType(method.ReturnType)));
            }
            if (value is IEnumerable)
            {
                return(FormatEnumerable(value));
            }
            return(value.ToString());
        }