FxCop - CA1034 错误 - 为什么?

发布于 2024-07-27 13:20:34 字数 1164 浏览 1 评论 0原文

我正在使用 FxCop 1.36 运行静态代码分析,并且不断收到 警告 CA1034:嵌套类型不应该可见。

我会理解父类是否被声明为内部或私有,但它是公共的。 为什么将 TimerReset 公开会不好?

我是否遗漏了一些东西,或者这是可以忽略的东西?

感谢您的任何意见!

以下是导致此警告的代码摘录:

namespace Company.App.Thing
{
    public partial class Page : XtraPage
    {
        public delegate void TimerResetDelegate(object sender, EventArgs e);
        private TimerResetDelegate _timerReset;

        public Page()
        {
            InitializeComponent();
        }

        public TimerResetDelegate TimerReset
        {
            set
            {
                if (null != (_timerReset = value))
                {
                    checkBox.Click += new EventHandler(_timerReset);
                    textField.Click += new EventHandler(_timerReset);
                    textField.KeyDown += new KeyEventHandler(_timerReset);
                    TimeField.Click += new EventHandler(_timerReset);
                    TimeField.KeyDown += new KeyEventHandler(_timerReset);
                }
            }
        }
    }
}

I am running static code analysis with FxCop 1.36 and I keep getting warning CA1034: NestedTypesShouldNotBeVisible.

I would understand if the parent class were declared as internal or private, but it is public. Why would it be bad for TimerReset to be declared public?

Am I missing something, or is this something that can be ignored?

Thanks for any input!

Here is an excerpt of the code causing this warning:

namespace Company.App.Thing
{
    public partial class Page : XtraPage
    {
        public delegate void TimerResetDelegate(object sender, EventArgs e);
        private TimerResetDelegate _timerReset;

        public Page()
        {
            InitializeComponent();
        }

        public TimerResetDelegate TimerReset
        {
            set
            {
                if (null != (_timerReset = value))
                {
                    checkBox.Click += new EventHandler(_timerReset);
                    textField.Click += new EventHandler(_timerReset);
                    textField.KeyDown += new KeyEventHandler(_timerReset);
                    TimeField.Click += new EventHandler(_timerReset);
                    TimeField.KeyDown += new KeyEventHandler(_timerReset);
                }
            }
        }
    }
}

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

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

发布评论

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

评论(5

浪漫人生路 2024-08-03 13:20:34

一般来说,嵌套类型更难“发现”。

例如,要使用嵌套类型,我必须编写以下内容

Page.TimerResetDelegate timer = new Page.TimerResetDelegate();

尽管上面是有效的 C# 代码,但它读起来不像通常的类型用法。

当您想要定义要在内部使用的类型并且您会避免像上面这样的代码时,通常使用嵌套类型。 这就是 FxCop 向您发出警告的原因。 如果您愿意,可以忽略它。 就我个人而言,我会将嵌套类型保留为私有类型。 如果我希望调用者使用该类型,我会将它们移动到适当的名称空间。

Generally speaking, Nested Types are harder to 'discover'.

E.g. To use your nested type, I will have to write following

Page.TimerResetDelegate timer = new Page.TimerResetDelegate();

Even though above is valid C# code, it doesn't read like the usual type usage.

Nested Types are generally used when you want to define a type to be used internally and you would avoid code like above. This is the reason why FxCop is giving you warning. If you want, you can ignore it. Personally, I would keep my Nested types as private. If I expect caller to make use of the type, I will move them to a proper namespace.

佞臣 2024-08-03 13:20:34

为什么将 TimerReset 公开会不好?

正如描述所述

嵌套类型对于封装包含类型的私有实现细节很有用。 用于此目的时,嵌套类型不应在外部可见。

由于您使用 TimerReset 公开公开 TimerResetDelegate,我想这不是实现细节。

不要使用外部可见的嵌套类型进行逻辑分组或避免名称冲突; 相反,使用命名空间。

这使得您看起来像是在使用嵌套类型进行分组。 正如 FxCop 所说,请使用命名空间。

嵌套类型包括成员可访问性的概念,但一些程序员并不清楚。

由于 TimerResetDelegate 是一个委托,因此这并不真正适用。

TimerResetDelegate 移至其自己的 TimeResetDelegate.cs 文件,并将其放入您的 Company.App.Thing 命名空间中。 然后,它就不再嵌套了。

当然,最好使用 EventHandler 而不是定义您自己的委托类型。

Why would it be bad for TimerReset to be declared public?

Exactly as the description states:

Nested types are useful for encapsulating private implementation details of the containing type. Used for this purpose, nested types should not be externally visible.

Since you're exposing TimerResetDelegate publically with TimerReset, I guess it's not an implementation detail.

Do not use externally visible nested types for logical grouping or to avoid name collisions; instead, use namespaces.

Which makes it look like you're using a nested type for grouping. As FxCop states, use a namespace instead.

Nested types include the notion of member accessibility, which some programmers do not understand clearly.

Since TimerResetDelegate is a delegate, this doesn't really apply.

Move TimerResetDelegate to it's own TimeResetDelegate.cs file, and put it in your Company.App.Thing namespace. Then, it's no longer nested.

Of course, it'd be even better to just go with EventHandler instead of defining your own delegate type.

萌︼了一个春 2024-08-03 13:20:34

这是因为您的委托是一种类型,但它是在 Page 类中定义的。 我只是在 Company.App.Thing 命名空间中定义它,但这实际上不是问题。 如果你正在编写一个 API,它只会让它变得有点混乱,仅此而已。

另外,像这样返回委托有点奇怪,但我想我真的不知道你想要完成什么。

It is because your delegate is a type, but it is defined within the Page class. I would just define it in the Company.App.Thing namespace, but it is not a problem really. If you were writing an API it would just make it a bit messy, that's all.

Also, it is a bit odd to return the delegate like that, but I guess I don't really know what you are trying to accomplish.

我不吻晚风 2024-08-03 13:20:34

恕我直言,这是一条可以忽略的 FxCop 规则。

从 CLR 级别来看,拥有嵌套类并没有什么问题。 这只是添加到 FxCop 中的指导规则,因为作者认为它比使类非嵌套更不可用或设计更差。

IMHO, this is an FxCop rule that can be ignored.

There is nothing wrong from a CLR level with having a nested class. It is just a guideline rule added to FxCop because the authors felt it was less usable or a poorer design than making the class non-nested.

恍梦境° 2024-08-03 13:20:34

显然,它不喜欢嵌套类的想法,因为它们可能在 Page 类的上下文之外使用。

我个人原则上同意这一点,尽管我可以想象一些可能需要的例外情况。

Apparently it doesn't like the idea of nested classes, when they might be used outside the context of your Page class.

I personally agree with that in principle, though I can imagine some exceptions where it might be desirable.

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