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

后端 未结 6 1767
青春惊慌失措
青春惊慌失措 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 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();
    

提交回复
热议问题