How can I determine if an object can ToString into value or type name?

后端 未结 6 1711
青春惊慌失措
青春惊慌失措 2021-01-19 09:38

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

相关标签:
6条回答
  • 2021-01-19 09:56

    Option 1: make sure that every Object will overwrite ToString(). Option 2: Use reflection to get all object properties and concat them.

    0 讨论(0)
  • 2021-01-19 10:01

    ...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) */
    }
    
    0 讨论(0)
  • 2021-01-19 10:07

    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));
            }
        }
    
    0 讨论(0)
  • 2021-01-19 10:10

    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.

    0 讨论(0)
  • 2021-01-19 10:11

    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();
    
    0 讨论(0)
  • 2021-01-19 10:21

    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

    0 讨论(0)
提交回复
热议问题