C# 从现有枚举创建新枚举
我有一个现有的枚举C# 从现有枚举创建新枚举,c#,enums,C#,Enums,我有一个现有的枚举 public enum BalanceType // existing enum { Available, Phone, Commissary, Account, Reserve, Encumber, Debt, Held, } 现在我想从中创建一个新的枚举。新字段仅包含两个字段 public class IvrBalanceInfo { public decimal Amount { get; se
public enum BalanceType // existing enum
{
Available,
Phone,
Commissary,
Account,
Reserve,
Encumber,
Debt,
Held,
}
现在我想从中创建一个新的枚举。新字段仅包含两个字段
public class IvrBalanceInfo
{
public decimal Amount { get; set; }
public IvrBalanceType Type { get; set; }
public IvrBalanceInfo(BalanceInfo info, BalanceType type)
{
Amount = info.Amount;
//How to create the enum IvrBalanceType?
}
public enum IvrBalanceType // new enum
{
Available,
Phone,
}
}
我的问题是如何快速绘制地图?我想把旧的换成新的。旧的是来自第三方的。元素太多了。我只需要两个。您可以尝试以下操作:
public class IvrBalanceInfo
{
public decimal Amount { get; set; }
public IvrBalanceType Type { get; set; }
public IvrBalanceInfo(BalanceInfo info, BalanceType type)
{
Amount = info.Amount;
Type = (IvrBalanceType)(int)type;
}
public enum IvrBalanceType // new enum
{
Available,
Phone,
}
}
我认为您应该检查值或获取其他值的默认值
只是确保它可以编译,没有时间检查它是如何工作的
public class IvrBalanceInfo
{
public decimal Amount { get; set; }
public IvrBalanceType Type { get; set; }
public IvrBalanceInfo(BalanceInfo info, BalanceType type)
{
Amount = info.Amount;
if (type == BalanceType.Available)
Type = IvrBalanceType.Available;
else if(type == BalanceType.Phone)
Type = IvrBalanceType.Phone
// here you have to handle the other values and set the default
// values for the Type Property or it will take the default value
// if you not set it
}
public enum IvrBalanceType // new enum
{
Available,
Phone,
}
}
另一种解决方案的问题是,如果
type
参数的值不是BalanceType.Available
,BalanceType.Phone
的值之一,它将抛出异常。问题是什么?你是什么意思?您不能创建枚举(除了使用反射发射生成程序集等非常丑陋的事情)。但更重要的是,你不应该做这样的事情。你到底想做什么?你是在问如何将一个enum
映射到另一个吗?嗯,你的意思是“我如何从BalanceType
转换到IvrBalanceType
”?是的,我想转换它@阿尔比雷奥