了解 CLS 合规性和正确的代码

发布于 2024-07-13 23:32:32 字数 1152 浏览 4 评论 0原文

我尝试创建一个抽象控件来管理应用程序中的某些状态。 然而,我遇到了一些 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> 参数。 怎么会? 我该如何解决这个问题以删除警告?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抹茶夏天i‖ 2024-07-20 23:32:32

查看您的代码,枚举似乎是称为枚举器的类的一部分。 该类未在您的代码中列出,但我假设您可以完全控制它。

该类还需要使用符合 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.

吐个泡泡 2024-07-20 23:32:32

要删除警告,请添加以下属性,使类、方法和属性如下所示:

<CLSCompliant(False)> _
Public MustInherit Class Fields
    Inherits System.Web.UI.UserControl

    <CLSCompliant(False)> _
    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

    <CLSCompliant(False)> _
    Protected MustOverride Sub ToggleState(ByVal state As FormState)
End Class

这向编译器表明您希望删除警告,并且您知道您的代码不符合 CLS 标准。

To remove the warnings add the following attributes so that the class, method and property look like this:

<CLSCompliant(False)> _
Public MustInherit Class Fields
    Inherits System.Web.UI.UserControl

    <CLSCompliant(False)> _
    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

    <CLSCompliant(False)> _
    Protected MustOverride Sub ToggleState(ByVal state As FormState)
End Class

This signifies to the compiler that you want the warnings removed and that you're aware your code is not CLSCompliant.

心意如水 2024-07-20 23:32:32

可能是您没有值为 0 的项目。

It could be that you do not have an item with value 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文