I am writing an interop between a php service and our crm. One of the things I need to do is make sure that simple types get converted ToString() for use later in a json co
Option 1: make sure that every Object will overwrite ToString(). Option 2: Use reflection to get all object properties and concat them.
...way to determine if an object of unknown type will ToString() into an expected value, and not the type name...
The default implementation of ToString()
on object
, according to documentation, returns "the fully qualified name of the object's type".
So we could come up with the hypothesis that whenever ToString() is overridden, its output will be "useful" in the sense you specified in the question.
To detect whether a function called is an override, we can make use of this answer, like so:
if(typeof(ObjectX).GetMethod("ToString").DeclaringType == typeof(ObjectX))
{
/* ObjectX has overridden ToString() */
}
else
{
/* ObjectX has inherited ToString() from its base class(es) */
}
I Think this is what you are looking, in the GetMethod the second argument is an empty array to watch for the .ToString(), just convert the i.GetType().GetMethod("ToString", new Type[] { }).DeclaringType == typeof(object)
to a function and there you go.
class Program
{
static void Main(string[] args)
{
int i = 55;
var s = "some string";
var x = new List<string>();
Console.WriteLine(i.ToString());
Console.WriteLine(i.GetType().GetMethod("ToString", new Type[] { }).DeclaringType == typeof(object));
Console.WriteLine(s.ToString());
Console.WriteLine(s.GetType().GetMethod("ToString",new Type[]{}).DeclaringType == typeof(object));
Console.WriteLine(x.ToString());
Console.WriteLine(x.GetType().GetMethod("ToString",new Type[]{}).DeclaringType == typeof(object));
}
}
The ToString
method is a virtual one and the default implementation is defined in the Object
class and simply returns the name of the type of the object:
public virtual string ToString()
{
return this.GetType().ToString();
}
int
for example, overrides this method to return a meaningful representation.
What you can do is use reflection to detect whether a type overrides the ToString
method like this:
public static bool OverridesToString(Type type)
{
return type.GetMethod("ToString", new Type[0]).DeclaringType != typeof(object);
}
If it does, there is a very good chance that the ToString
method would return something meaningful.
So you want to check whether a type has a overridden ToString
method? Why not just check whether the value returned by ToString
is equal to the value returned by the default implementation of ToString
?
From here, we know the default implementation of ToString
is
return GetType().ToString();
So, we can use this to check whether an object has overridden the ToString
method:
bool toStringOverridden = someObject.GetType().ToString() !=
someObject.ToString();
Maybe you can do something similar to this:
bool ToStringIsTyped<T>(T myObj)
{
return myObj.ToString().Contains(typeof(T).FullName);
}
It may not work in all cases, but possibly could be expanded