【发布时间】:2014-08-20 05:42:39
【问题描述】:
固件制造商时不时地发布一个 API,其中包含这样的类:
public static class Manufacturer
{
public const ushort FooInc = 1;
public const ushort BarInc = 2;
public const ushort BigCompany = 3;
public const ushort SmallCompany = 4;
public const ushort Innocom = 5;
public const ushort Exocomm = 6;
public const ushort Quarqian = 7;
// snip... you get the idea
}
这个类我没有控制权,而且很可能时不时会出一个新的,所以我真的不想重写它。
在我的代码中,我可以从数据文件中访问一个整数,该整数表示该文件来自的设备的“制造商”。
如果可能,我希望在我的 UI 上显示制造商的名称,而不是数字,但我能找到的唯一交叉引用是这个类。
那么,鉴于文件中的数字“6”,如何将其转换为文本“Exocomm”?
【问题讨论】:
-
我很确定唯一的方法是使用反射,枚举该类中的所有常量并检查哪一个具有给定值。最好使用
enum而不是类/常量。
标签: c#