Converting object of a class to of another one

前端 未结 7 696
佛祖请我去吃肉
佛祖请我去吃肉 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:42

    You could add an implicit type conversion operator:

    public class DoubleClass
    {
        public double X;
        public double Y;
        public double Z;
    
        public static implicit operator FloatClass(DoubleClass d)
        {
            return new FloatClass { X = (float)d.X, Y = (float)d.Y, Z = (float)d.Z };
        }
    }
    

    Now this works:

    DoubleClass doubleObject = new DoubleClass();
    FloatClass convertedObject = doubleObject;
    
    0 讨论(0)
  • 2020-12-28 08:43

    You can use Conversion Operators to achieve this.

    Fr example:

    struct FloatClass
    {
        public FloatClass(DoubleClass dClass) {
            //conversion...
        }
        ... 
        public static explicit operator FloatClass(DoubleClass dClass) 
        {
            FloatClassd = new FloatClass(dClass);  // explicit conversion
    
            return d;
        }
    }
    
    
    var convertedObject = (FloatClass)doubleObject;
    
    0 讨论(0)
  • 2020-12-28 08:44

    Sounds like you could use generics here:

     public class GenericClass<T>
     {
        T X { get; set; }
        T Y { get; set; }
        T Z { get; set; }
     }
    
     GenericClass<float> floatClass = new GenericClass<float>();
     GenericClass<double> doubleClass = new GenericClass<double>();
    
    0 讨论(0)
  • 2020-12-28 08:46

    Use a conversion operator:

    public static explicit operator FloatClass (DoubleClass c) {
       FloatCass fc = new FloatClass();
       fc.X = (float) c.X;
       fc.Y = (float) c.Y;
       fc.Z = (float) c.Z;
       return fc;
    }
    

    And then just use it:

    var convertedObject = (FloatClass) doubleObject;
    

    Edit

    I changed the operator to explicit instead of implicit since I was using a FloatClass cast in the example. I prefer to use explicit over implicit so it forces me to confirm what type the object will be converted to (to me it means less distraction errors + readability).

    However, you can use implicit conversion and then you would just need to do:

    var convertedObject = doubleObject;
    

    Reference

    0 讨论(0)
  • 2020-12-28 08:46

    Best way is Serialize object and again desalinize it

    0 讨论(0)
  • 2020-12-28 08:52

    The simplest way to do this is by using serializer. Use Newtonsoft JSON serializer which works best.

    using Newtonsoft.Json;

      private void Convert()
        {
            DoubleClass doubleClass = new DoubleClass {X = 123.123, Y = 321.321, Z = 111.111};
            var serializedoubleClass = JsonConvert.SerializeObject(doubleClass);
            var floatClass = JsonConvert.DeserializeObject(serializedoubleClass, typeof(FloatClass));
        }
    
    0 讨论(0)
提交回复
热议问题