Converting object of a class to of another one

前端 未结 7 697
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 08:33

I have two classes which have are nearly equal except the data types stored in them. One class contains all double values while other contains all float values.



        
相关标签:
7条回答
  • 2020-12-28 08:52

    Best way for Convert

    public static class Extention {
         public static string ConvertObjectToJson(this object ob)
         {
             return JsonConvert.SerializeObject(ob);
         }
    }
    

    For Usage

    var doubleClass = new DoubleClass {
       x = 10,
       y = 20
    };
    var floatClass = JsonConvert.DeserializeObject<FloatClass>(doubleClass.ConvertObjectToJson());
    
    0 讨论(0)
提交回复
热议问题