Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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? :-)

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

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top