[英]C# Copying Enumeration from one object to another
I have two very similar but not identical C# objects.我有两个非常相似但不相同的 C# 对象。 I am copying the values from one class to another.
我将值从一个 class 复制到另一个。
Each class has some properties that expose an enumeration type.每个 class 都有一些公开枚举类型的属性。 The inside of the enumerations are the same but the names are different eg
枚举的内部相同,但名称不同,例如
public enum EnumA
{
A,
B
}
public EnumA EnumAProperty
{
get{ return enumA;}
}
public enum EnumB
{
A,
B
}
public EnumB EnumBProperty
{
get{ return enumB;}
}
I want to assign the value returned from EnumBProperty to EnumAProperty is this possible?我想将从 EnumBProperty 返回的值分配给 EnumAProperty 这可能吗?
Each enum
member has a corresponding integer value.每个
enum
成员都有一个对应的 integer 值。
By default, these values are assigned in ascending order, starting with 0
.默认情况下,这些值按升序分配,从
0
开始。
If the order of the items in the enums (and thus their numeric values) are the same, you can just cast the numeric value to EnumB
to get the EnumB
member with the same value:如果枚举中项目的顺序(以及它们的数值)相同,您可以将数值转换为
EnumB
以获得具有相同值的EnumB
成员:
EnumBProperty = (EnumB)(int)EnumAProperty;
If not, you need to re-parse it:如果没有,您需要重新解析它:
EnumBProperty = (EnumB)Enum.Parse(typeof(EnumB), EnumAProperty.ToString());
You can do via casting but I would not recommend it as it is fragile — if any of the enum members are reordered or new items added the result may not be what you expect.你可以通过强制转换来做,但我不推荐它,因为它很脆弱——如果任何枚举成员被重新排序或添加了新项目,结果可能不是你所期望的。
EnumAProperty = (EnumA) EnumBProperty;
Even worse with the casting is if you have items in your source enum with no equivalent in the destination — below there are more colours than shapes:更糟糕的是,如果你的源枚举中有项目,而目标中没有对应的项目——下面的颜色比形状多:
enum Color { Red = 0, Yellow = 1, Blue = 2 };
enum Shape ( Square = 0, Triangle = 1 };
Color color = Color.Red;
Shape shape = (Shape) color;
shape
could end up with the value 2
even though this value is not defined.即使未定义此值,
shape
也可能以值2
结束。
Instead, I'd suggest you use a switch
statement to map:相反,我建议您对 map 使用
switch
语句:
EnumAProperty = ConvertToA(EnumBProperty);
...
private static EnumA ConvertToA(EnumBProperty b)
{
switch (b)
{
case EnumB.Flannel: return EnumA.HandTowel;
case EnemB.Vest: return EnumA.UnderShirt;
...
default: throw new ArgumentOutOfRangeException("b");
}
}
As long as both enum's of different types you can'not assign it directly.只要两个枚举类型不同,您就不能直接分配它。 You can define integer offset for an items so you can assign values through the integer value
您可以为项目定义 integer 偏移量,以便您可以通过 integer 值分配值
public enum EnumA
{
A = 0,
B = 1
}
public enum EnumB
{
A = 0,
B = 1
}
EnumBPropertry = (int)EnumAProperty
You could either cast it to an integer or convert it to a string and then do an Enum.Parse on it.您可以将其转换为 integer 或将其转换为字符串,然后对其执行 Enum.Parse。
Try the following:尝试以下操作:
EnumAProperty = (EnumA)Enum.Parse(typeof(EnumA), EnumBProperty.ToString);
EnumBProperty = (EnumB)Enum.Parse(typeof(EnumB), EnumAProperty.ToString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.