C# 将枚举从一个对象复制到另一个对象

发布于 2024-11-29 06:06:00 字数 334 浏览 3 评论 0原文

我有两个非常相似但不相同的 C# 对象。我正在将值从一个类复制到另一个类。

每个类都有一些公开枚举类型的属性。枚举的内部相同,但名称不同,例如

public enum EnumA
{
 A,
 B
} 

public EnumA EnumAProperty
{
 get{ return enumA;}
}

public enum EnumB
{
 A,
 B
} 

public EnumB EnumBProperty
{
 get{ return enumB;}
}

我想将从 EnumBProperty 返回的值分配给 EnumAProperty 这可能吗?

I have two very similar but not identical C# objects. I am copying the values from one class to another.

Each class has some properties that expose an enumeration type. The inside of the enumerations are the same but the names are different e.g.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不乱于心 2024-12-06 06:06:00

您可以通过强制转换来完成,但我不会推荐它,因为它很脆弱 - 如果任何枚举成员被重新排序或添加新项目,结果可能不是您所期望的。

EnumAProperty = (EnumA) EnumBProperty;

更糟糕的是,如果源枚举中有项目,但目标枚举中没有对应的项目 - 下面的颜色多于形状:

enum Color { Red = 0, Yellow = 1, Blue = 2 };
enum Shape ( Square = 0, Triangle = 1 };

Color color = Color.Red;
Shape shape = (Shape) color;

shape 最终可能得到值 2 即使该值未定义。

相反,我建议您使用 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");
    }
}

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.

Instead, I'd suggest you use a switch statement to map:

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");
    }
}
云淡月浅 2024-12-06 06:06:00

每个enum成员都有一个对应的整数值。
默认情况下,这些值按升序分配,从 0 开始。

如果枚举中项目的顺序(以及它们的数值)相同,您只需将数值转换为 EnumB 即可获取带有以下内容的 EnumB 成员:相同的值:

 EnumBProperty = (EnumB)(int)EnumAProperty;

如果不是,则需要重新解析它:

EnumBProperty = (EnumB)Enum.Parse(typeof(EnumB), EnumAProperty.ToString());

Each enum member has a corresponding integer value.
By default, these values are assigned in ascending order, starting with 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:

 EnumBProperty = (EnumB)(int)EnumAProperty;

If not, you need to re-parse it:

EnumBProperty = (EnumB)Enum.Parse(typeof(EnumB), EnumAProperty.ToString());
感情废物 2024-12-06 06:06:00

只要两个枚举的类型不同,就不能直接分配它。
您可以为项目定义整数偏移量,以便可以通过整数值分配值

public enum EnumA 
{  
 A = 0,  
 B = 1
}   

public enum EnumB
{  
 A = 0,  
 B = 1
}   

EnumBPropertry = (int)EnumAProperty

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

public enum EnumA 
{  
 A = 0,  
 B = 1
}   

public enum EnumB
{  
 A = 0,  
 B = 1
}   

EnumBPropertry = (int)EnumAProperty
软糯酥胸 2024-12-06 06:06:00

您可以将其转换为整数或将其转换为字符串,然后对其执行 Enum.Parse 。

You could either cast it to an integer or convert it to a string and then do an Enum.Parse on it.

白日梦 2024-12-06 06:06:00

请尝试以下操作:

EnumAProperty = (EnumA)Enum.Parse(typeof(EnumA), EnumBProperty.ToString);

Try the following:

EnumAProperty = (EnumA)Enum.Parse(typeof(EnumA), EnumBProperty.ToString);
好菇凉咱不稀罕他 2024-12-06 06:06:00
EnumBProperty = (EnumB)Enum.Parse(typeof(EnumB), EnumAProperty.ToString());
EnumBProperty = (EnumB)Enum.Parse(typeof(EnumB), EnumAProperty.ToString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文