C# object to string and back

前端 未结 3 2093
梦毁少年i
梦毁少年i 2021-02-05 16:13

My problem:
I have a dynamic codecompiler which can compile a snippet of code. The rest of the code. (imports, namespace, class, main function) is already there. The snipp

3条回答
  •  感动是毒
    2021-02-05 16:49

    You will have to make a conversion method to display it and serialize it to be able to convert it back and forth.

    For instance:

        public static string ConvertToDisplayString(object o)
        {
            if (o == null)
                return "null";
            var type = o.GetType();
            if (type == typeof(YourType))
            {
                return ((YourType)o).Property;
            }
            else
            {
                return o.ToString();
            }
        }
    

提交回复
热议问题