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());
}