了解 CLS 合规性和正确的代码
我尝试创建一个抽象控件来管理应用程序中的某些状态。 然而,我遇到了一些 CLS 问题,希望有人能提供一些见解。
我有一个这样的枚举:
<Flags()> _
Public Enum FormState
Read = 1
Edit = 2
Insert = 4
End Enum
和一个这样的类:
Public MustInherit Class Fields
Inherits System.Web.UI.UserControl
Public Property State() As Enumerators.FormState
Get
Return _State
End Get
Set(ByVal value As Enumerators.FormState)
_State = value
ToggleState(value)
End Set
End Property
Protected MustOverride Sub ToggleState(ByVal state As FormState)
End Class
当我尝试编译此代码时,我收到一条警告,指出 State 属性不符合 CLS,state< /em> 参数。 怎么会? 我该如何解决这个问题以删除警告?
- 我尝试添加
; 归因于这两个项目,但没有运气 - 我试图传播 MSDN 文章非-CLS 兼容的“MustOverride”成员不允许在符合 CLS 的 代码中使用,但没有结果
- 我尝试将访问器更改为
Friend
而不是Public
- 我尝试指定枚举的类型(
Integer
和UInteger
)
I've attempted to create an abstracted control to manage some of the state in our application. However, I have run a foul of some CLS issues and was hoping that someone could provide some insight.
I have an enumeration as such:
<Flags()> _
Public Enum FormState
Read = 1
Edit = 2
Insert = 4
End Enum
And a class as such:
Public MustInherit Class Fields
Inherits System.Web.UI.UserControl
Public Property State() As Enumerators.FormState
Get
Return _State
End Get
Set(ByVal value As Enumerators.FormState)
_State = value
ToggleState(value)
End Set
End Property
Protected MustOverride Sub ToggleState(ByVal state As FormState)
End Class
When I attempt to compile this code I am left with a warning that the State property is not CLS compliant and neither is the state argument. How come? And how can I correct this problem to remove the warnings?
- I've attempted to add the <CLSCompliant(True)> attribute to both items with no luck
- I tried to disseminate the MSDN article Non-CLS-compliant 'MustOverride' member is not allowed in a CLS-compliant into the code with no results
- I've tried changing the accessors to
Friend
instead ofPublic
- I've tried specifying a type for the Enum (
Integer
andUInteger
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看您的代码,枚举似乎是称为枚举器的类的一部分。 该类未在您的代码中列出,但我假设您可以完全控制它。
该类还需要使用符合 CLS 的属性进行标记。
Looking at your code, the enum seems to be part of a class called enumerators. The class is not listed in your code, but I'm assuming that you have full control over it.
The class needs to be tagged with the CLS compliant attribute as well.
要删除警告,请添加以下属性,使类、方法和属性如下所示:
这向编译器表明您希望删除警告,并且您知道您的代码不符合 CLS 标准。
To remove the warnings add the following attributes so that the class, method and property look like this:
This signifies to the compiler that you want the warnings removed and that you're aware your code is not CLSCompliant.
可能是您没有值为 0 的项目。
It could be that you do not have an item with value 0.