How to determine if a ToString object can assign a value or a type name?

I am writing interop between php service and our crm. One of the things I need to do is make sure that simple types are converted to ToString () for use later in the json converter.

I’m not even sure what the name is for "simple types", but it can be defined as follows ... "an object that represents the type of a low-level variable containing a single value, not a class or anything with an executable function, etc. d.

I found that int, string, bool, double and surprisingly listed would be ToString () with pretty predictable results.

int x = 0;
bool y = true;
double z = 1.59 // money
CustomEnum theEnum = CustomEnum.somevalue;

x.ToString () results in "0"

y.ToString () yields true

z.ToString () results in "1.59"

theEnum.ToString () results in "somevalue"

:

List<int> iList = new List<int>();
iList.Add(1);

MyClass theClass = new MyClass();

iList.ToString() "System.Collections.Generic.List`1 [System.Int32]" theClass.ToString() "STTI.NKI.Interop.MyClass"

. ExpandoObject, ..

, , , , ToString() , . , -

switch (theObject.GetType())
 case typeof(int):
 case typeof(bool):
 case typeof(doulble):
etc

, , .

+4
6

, , ToString? , , ToString, , ToString?

, , ToString

return GetType().ToString();

, , , ToString:

bool toStringOverridden = someObject.GetType().ToString() !=
    someObject.ToString();
+4

ToString , Object :

public virtual string ToString()
{
  return this.GetType().ToString();
}

int, , .

, , , ToString:

public static bool OverridesToString(Type type)
{
    return type.GetMethod("ToString", new Type[0]).DeclaringType != typeof(object);
}

, , ToString - .

+3

1: , ToString(). 2: , .

0

, - :

bool ToStringIsTyped<T>(T myObj)
{
    return myObj.ToString().Contains(typeof(T).FullName);
}

, , ,

0

, , , GetMethod .ToString(), i.GetType().GetMethod("ToString", new Type[] { }).DeclaringType == typeof(object) .

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

... , ToString() , ...

The default implementation of ToString()on object, according to the documentation , returns "the full name of the object type."

So, we could come up with the hypothesis that whenever ToString () is redefined, its output will be β€œuseful” in the sense that you asked in the question.

To determine if the called function is an override, we can use this answer , for example:

if(typeof(ObjectX).GetMethod("ToString").DeclaringType == typeof(ObjectX))
{
    /* ObjectX has overridden ToString() */
}
else
{
    /* ObjectX has inherited ToString() from its base class(es) */
}
0
source

Source: https://habr.com/ru/post/1654834/


All Articles