我有一个为我的设计定义常量的类。例如以下:
public static class ObjectTypes
{
/// <summary>
/// The identifier for the ConfigurableObjectType ObjectType.
/// </summary>
public const uint ConfigurableObjectType = 2;
/// <summary>
/// The identifier for the FunctionalGroupType ObjectType.
/// </summary>
public const uint FunctionalGroupType = 4;
/// <summary>
/// The identifier for the ProtocolType ObjectType.
/// </summary>
public const uint ProtocolType = 5;
}
现在在我的代码中,我已经计算了.eg valueInt 的整数值,我想将 valueInt 与此类中定义的所有常量进行比较。有没有一种不使用 If-then-else 块或 switch case 的快速方法,因为如果有大量常量,这种方法将导致大量代码。以某种方式有更好的方法吗?我在 C# 中工作。
注意:我无法更改上述类的设计,因为我从库或其他人设计的类中获得了预定义的类,我无法更改但我只能在我的代码中引用。