CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Dynamic - Abuse or Win?

336 Views
Copy Code Show/Hide Line Numbers
 
// la la la
if (foo is ISomeInterface) {
    ((ISomeInterface)foo).SomeMethod();
}
else if (foo is ISomeOtherInterface) {
    ((ISomeOtherInterface)foo).SomeMethod();
}
else {
    throw new SomeException();
}
 
// or, just abuse dynamic! :)
((dynamic)foo).SomeMethod();
by InfinitiesLoop
  August 09, 2010 @ 4:26pm
Tags:

by Jeff Handley    August 09, 2010 @ 4:46pm

I call that a perfect use of dynamic.

by Rick Strahl    August 11, 2010 @ 5:27pm

Depends on the scenario. If you know what interfaces/types you're expecting you should always try to use them. Dynamic is perfect if you truly don't know and you can justify of the overhead of dynamic.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate