C# - 禁用动态关键字

发布于 2024-08-31 22:06:37 字数 100 浏览 10 评论 0原文

有什么方法可以禁用 .net 4 中“dynamic”关键字的使用吗?

我认为 VS2010 的代码分析功能可能有一个规则,如果使用动态关键字,则构建失败,但我无法改进。

Is there any way to disable the use of the "dynamic" keyword in .net 4?

I thought the Code Analysis feature of VS2010 might have a rule to fail the build if the dynamic keyword is used but I couldn't fine one.

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

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

发布评论

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

评论(5

鹿! 2024-09-07 22:06:37

它是 C# 4.0 语言的一部分,所以不完全是。

您可以使用 FXCop 来查找它,如果出现以下情况,则构建失败它遇到了它。

Style cop 可能会起作用:

http://code.msdn.microsoft.com/sourceanalysis

这里是一个讨论同一问题以及风格警察如何可能成为答案的链接。还有一篇关于如何让 FX 警察潜在地寻找动态关键字的帖子,尽管它并不完美。

http://social.msdn。 microsoft.com/Forums/en/vstscode/thread/8ce407ba-bdf7-422b-bbcd-ca4701c3a76f

It's part of the C# 4.0 language, so no not really.

You can use FXCop to look for it though and fail the build if it encounters it.

Style cop might work instead:

http://code.msdn.microsoft.com/sourceanalysis

Here is a link talking about the same issue and how style cop might be the answer. There is also a post about how to get FX cop to potentially look for the dynamic keyword, although it's not perfect.

http://social.msdn.microsoft.com/Forums/en/vstscode/thread/8ce407ba-bdf7-422b-bbcd-ca4701c3a76f

笑忘罢 2024-09-07 22:06:37

动态关键字并不是邪恶的,但使用它可能是邪恶的。

它会导致您只能在运行时才能发现的代码错误。
应不惜一切代价避免这种情况。
运行时错误是很糟糕的。编译时错误是好的。

您可以使用类似以下内容来设置自己的标准。
http://joel.fjorden.se/static.php?page=CodeStyleEnforcer

The dynamic keyword is not evil, but using it could be.

It leads to code errors that you can only find during runtime.
This should be avoided at all costs.
Runtime errors are bad. Compile time errors are good.

You could use something like the following to set your own standards.
http://joel.fjorden.se/static.php?page=CodeStyleEnforcer

千鲤 2024-09-07 22:06:37

目标.net 1.0? :-)

或者进行代码审查。

(或者,不那么开玩笑,编写自定义 FxCop 或 CA 规则来禁止使用动态应该很容易)

您现在不就杀掉一个 C++ 宏吗? :-)

Target .net 1.0? :-)

Or do code reviews.

(Or, to be less facetious, it should be pretty easy to write a custom FxCop or CA rule to disallow use of dynamic)

Wouldn't you just kill for a C++ macro right now? :-)

洋洋洒洒 2024-09-07 22:06:37

删除对 Microsoft.CSharp.dll 的引用,我认为也许所有使用 dynamic 的操作都将无法编译。

Remove the reference to Microsoft.CSharp.dll, and I think maybe all uses of dynamic will fail to compile.

梦初启 2024-09-07 22:06:37

我不确定我是否理解这种对动态关键字的非理性恐惧是为了什么。对匿名变量和 .NET 3.5 的 var 关键字存在这种类型的歇斯底里,只不过这很愚蠢,因为这些是合法的静态定义类型。

动态关键字具有高度专业化的目的,我不明白为什么有人会在不理解原因的情况下想要使用它。然而,阻止这种情况发生可以通过 1 次团队会议来解决,该会议解释了 .NET 4 的一些新功能,包括动态关键字。我认为你更像是团队的资深人士或高级领导者;如果您的团队觉得需要使用动态关键字来首先见您,应该很容易告诉他们。

这正是我给我的团队的指示,因为我发现我们不太可能使用动态关键字,因为我们不编写 COM 互操作活动。除此之外,我将把任何类型的动态代理使用推迟到像 Linfu 或 Castle 这样的已建立的库,并将动态代理的实现留给他们来使用或不使用动态关键字。

I'm not sure I understand what this irrational fear of the dynamic keyword is for. There was this type of hysteria over anonymous variables and the var keyword for .NET 3.5 except that was just idiotic since those are legitimate statically defined types.

The dynamic keyword serves a highly specialized purpose, I don't see why any person would have the desire to use it without understanding why. However stopping that from occurring could be solved with 1 team meeting explaining some of the new features of .NET 4 including the dynamic keyword. I assume you're more of a senior or the senior lead of the team; it should be quite easy to tell your team if they ever feel they NEED to use the dynamic keyword to come see you FIRST.

This was exactly the instructions I gave to my team as I find it unlikely we will ever use the dynamic keyword because we don't write COM interop activity. And past that I will defer any type of dynamic proxy use to an established library like Linfu or Castle and leave up the implementation of dynamic proxies to them to use or not use the dynamic keyword.

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