C# 中隐藏的枚举值
我有一个枚举,我想“隐藏”它的一个值(因为我添加它是为了将来的支持)。 该代码是用 C# 编写的。
public enum MyEnum
{
ValueA = 0,
ValueB = 1,
Reserved
}
我不想允许使用此代码的人使用此值(MyEnum.Reserved)。 有什么想法吗? TIA
I have an enum and i want to "hide" one of its values (as i add it for future support).
The code is written in C#.
public enum MyEnum
{
ValueA = 0,
ValueB = 1,
Reserved
}
I don't want to allow peoples who use this code to use this values (MyEnum.Reserved).
Any idea?
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用“Obsolete”属性 - 语义上不正确,但它会执行您想要的操作:
任何尝试使用
Foo.Reserved
项进行编译的人都会收到错误You could use the 'Obsolete' attribute - semantically incorrect, but it will do what you want:
Anyone who tries to compile using the
Foo.Reserved
item will get an error如果您不想显示它,则不要包含它:
请注意,此枚举的用户仍然可以将任何整数值分配给声明为 MyEnum 的变量:
这意味着接受枚举的方法应始终验证此值使用前输入:
因此,只需稍后在需要时添加您的值,然后修改您的方法以接受它。
If you don't want to show it, then don't include it:
Note that a user of this enum can still assign any integer value to a variable declared as MyEnum:
This means that a method that accepts an enum should always validate this input before using it:
So, just add your value later, when you need it, and modify your methods to accept it then.
这在 C# 中是不可能的。如果
Enum
本身可访问,则所有枚举值都可访问。模拟实现此目的的唯一方法是使用不易访问的静态字段,该字段使用
Enum
中尚未使用的整数值。我很好奇你为什么要这样做。无论您做什么,用户仍然可以提供
Reserved
值。需要做的就是将适当值的int
转换为MyEnum
类型。This is not possible in C#. All enum values are accessible if the
Enum
itself is accessible.The only way you could simulate accomplishing this is by using a less accessible static field that used an integer value not already used in the
Enum
.I'm curious though as to why you would want to do this. No matter what you do users can still provide the
Reserved
value. All that needs to be done is to cast anint
of the appropriate value to theMyEnum
type.如果要隐藏智能感知或 PropertyGrid 枚举成员,可以应用:
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
和
[System.ComponentModel.Browsable(false)]
示例:
C 不可见
if you want to hide from intellisense or PropertyGrid enumerated members, you can apply:
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
and
[System.ComponentModel.Browsable(false)]
Example:
C is not visible
您可以通过使用自己的自定义类型而不是枚举来实现类似的效果:
您甚至可以通过以下方式进一步模拟枚举值的行为 :例如,重载
explicit
运算符以将MyEnum
对象转换为int
值(采纳 JaredPar 的建议,使用此运算符而不是implicit) ):
You can achieve something like this by using your own custom type instead of
enum
:You could even further emulate the behavior of
enum
values by, for example, overloading theexplicit
operators to convert to/fromMyEnum
objects toint
values (took JaredPar's suggestion to use this rather thanimplicit
):我对枚举标志也有类似的问题,但我使用的解决方案应该在没有标志属性的情况下工作。
您可以创建两种枚举类型,一种用于保留值,另一种用于公共可用值。您可以将 HasFlag 与保留类型一起使用而无需强制转换,但赋值需要强制转换。
I had a similar problem with enum Flags, but the solution I used should work without the Flags attribute.
You can create two enum types, one for the reserved values and the other for the publicly usable values. You can use HasFlag with the reserved type without casting, but assignment requires casting.
您无法隐藏它,但您可以添加注释,表明该值目前没有任何作用。否则,只需删除它并在添加支持时添加它,只要原始值不改变,这就不应该破坏依赖于它的任何其他代码。
You can't hide it, but you can add a comment that this value doesn't have any effect at this moment. Otherwise just remove it and add it if you add the support, this shouldn't break any other code dependent on it, as long as the original values don't change.
您在内部使用“隐藏”值吗?如果不是:
定义未使用的变量不会带来任何好处。
Are you using the "hidden" value internally? If not:
You gain nothing by defining an unused variable.
实现此目的的一种方法是设置此变量
null
的值。因此,当从
enum
调用它时,它将为 null。简而言之用户无法访问它的值。one way you can do this set the value of this variable
null
.so when ever its called from
enum
it'll b null. in short user can't access its value.