views:

313

answers:

6

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.

+4  A: 

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

Kevin
I will try this.
chief7
turns out FxCop can find dynamic class-level fields, method params, and method returns via the DynamicAttribute but can not find local variables inside methods b/c they are converted to objects without any attribute.
chief7
@cheif7 I did some more looking and style cop might be the way to go. I posted a link to some posts about the same problem.
Kevin
@Kevin - Thats my post. StyleCop looks like it will work well. Thanks for the help.
chief7
+2  A: 

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

Jason Williams
#define dynamic object
Hans Passant
+1  A: 

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

Patrick
Reflection does that too. Nobody ever bans reflection though, its just sort of understood that it is a very specific tool for a very specific job, and sometimes the perf hit and the lack of compile time errors are worth it, cause there is literally no other way to do certain kinds of things. Ditto with dynamic. Using it everywhere by default is moronic, but at the same time, there are certain types of things where the tradeoff is worth it.
Matt Briggs
+1  A: 

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

Brian
+2  A: 

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.

Chris Marisic
A: 

Can't comment, but:

Chris Marisic, the problem is uneducated RAD - victims and wannabe renegades who think that writing code in a "zomg dynamic" language is "1337 kewl".

We will see even the strategy pattern "implemented" with the dynamic keyword, because many "developers" (well, they are not really devs, but they call themselves devs...) will go "hey, dynamic saves we the work of actually defining a contract and I can still get the job done - and since I get paid for that, I don't care about the future, this is only a job".

And people like me will have to live with the consequences.

stormianrootsolver