简体   繁体   English

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

[英]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.

相关问题 使用C#上的参数从一个数据库复制行中的行 - Copying rows from one database from another with parameters on C# 使用LINQ和C#将行从一个表复制到另一个表 - Copying a row from one table to another using LINQ and C# C#将多个XMLNode或XMLNodeList从一个XMLDocument复制到另一个 - C# Copying Multiple XMLNodes or XMLNodeList from One XMLDocument to Another 使用C#将面板从一种形式复制到另一种形式 - Copying Panels from one Form to another in C# C#将文件从一台服务器复制到另一台具有权限的服务器 - C# Copying a file from one server to another with permissions 将一个数据表复制到另一数据表C# - Copying one datatable to another datatable c# 使用 C# 将数据从一个 dgv 复制到另一个具有值和计数的 dgv - Copying data from one dgv to another dgv with values and count using C# C# - 将属性值从一个实例复制到另一个实例,不同的类 - C# - copying property values from one instance to another, different classes 如何共享将图像从一个文件夹复制到另一个文件夹的过程 - how to share image copying from one folder to another folder Process in c# 使用C#将数据从一个Oracle数据库复制到另一个Oracle数据库 - Copying data from one oracle database to another oracle database using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM