C# 将一个类的对象转换为另一个类的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18784274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting object of a class to of another one
提问by fhnaseer
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.
除了存储在其中的数据类型之外,我有两个几乎相等的类。一个类包含所有双精度值,而其他类包含所有浮点值。
class DoubleClass
{
double X;
double Y;
double Z;
}
class FloatClass
{
float X;
float Y;
float Z;
}
Now I have a point of DoubleClass which I want to convert to FloatClass.
现在我有一个 DoubleClass 点,我想将其转换为 FloatClass。
var doubleObject = new DoubleClass();
var convertedObject = (FloatClass)doubleObject; // TODO: This
One simple way is to make a method which creates a new FloatClass object, fills all values and return it. Is there any other efficient way to do this.
一种简单的方法是创建一个方法来创建一个新的 FloatClass 对象,填充所有值并返回它。有没有其他有效的方法来做到这一点。
采纳答案by letiagoalves
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).
由于我在示例中使用了强制转换,因此我将运算符更改为explicit
而不是。我更喜欢使用over所以它迫使我确认对象将被转换成什么类型(对我来说这意味着更少的干扰错误+可读性)。implicit
FloatClass
explicit
implicit
However, you can use implicit
conversion and then you would just need to do:
但是,您可以使用implicit
转换,然后您只需要执行以下操作:
var convertedObject = doubleObject;
回答by Tigran
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;
回答by Darren
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>();
回答by Tim Schmelter
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;
回答by Rupesh Dahibavkar
Best way is Serialize object and again desalinize it
最好的方法是序列化对象并再次对其进行脱盐
回答by Aamol
The simplest way to do this is by using serializer. Use Newtonsoft JSON serializer which works best.
最简单的方法是使用序列化程序。使用效果最好的 Newtonsoft JSON 序列化程序。
using Newtonsoft.Json;
使用 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));
}
回答by Mohammad
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());