Welcome to MSDN Blogs Sign in | Join | Help

New Article by Bill Wagner on Dynamic Method Bags in C# 4.0

C# MVP and wunderkind Bill Wagner has written an article entitled Dynamic Method Bags which is now available on MSDN. Bill explores the new dynamic feature in C# 4.0. Most posts on this subject explain how to use dynamic to call Python, Ruby or Office. In his article, Bill explains how dynamic can be used not to call another language or tool, but to call your own C# objects without directly using the reflection API’s. The solution he provides combines generics, expression tress and dynamic to create a “type that allows developers to add new methods at runtime” and call those methods through dynamic dispatch.

Resources

kick it on DotNetKicks.com
Published Tuesday, November 03, 2009 2:30 PM by Charlie Calvert
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: New Article by Bill Wagner on Dynamic Method Bags in C# 4.0

The code download link is not wired up.  Looking forward to trying this out once the download is available.

Wednesday, November 04, 2009 6:54 AM by Dave

# re: New Article by Bill Wagner on Dynamic Method Bags in C# 4.0

That's all good, but he doesn't explain why I would want to use one, over and above the extremely simple case shown (overkill!).

Wednesday, November 04, 2009 8:13 AM by Robinson

# re: New Article by Bill Wagner on Dynamic Method Bags in C# 4.0

this is an expression bag... not a method bag.

Thursday, November 12, 2009 6:55 AM by dr.no

# re: New Article by Bill Wagner on Dynamic Method Bags in C# 4.0

Why not use the ExpandoObject class instead?

dynamic contact = new ExpandoObject();

contact.Name = "Patrick Hines";

contact.Phone = "206-555-0144";

contact.Address = new ExpandoObject();

contact.Address.Street = "123 Main St";

contact.Address.City = "Mercer Island";

contact.Address.State = "WA";

contact.Address.Postal = "68402";

http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject%28VS.100%29.aspx

Monday, November 23, 2009 11:36 AM by CoryC

# re: New Article by Bill Wagner on Dynamic Method Bags in C# 4.0

Cory,

I think one of the points of Bill's articles, and his work in general, is to explain the syntax of new features in C#. The ExpandoObject is practical and useful, but it is nice to step behind the scense and see how things work. Bill is particularly good at that, and he does an excellent job of explaining the new Dynamic syntax in this article. Thanks though, for mentioning the ExpandoObject, as it is an important part of this story.

- Charlie

Tuesday, November 24, 2009 8:35 AM by Charlie Calvert

# re: New Article by Bill Wagner on Dynamic Method Bags in C# 4.0

why on earth does lots fo pages published through MSDN contain javascript errors...

Wednesday, December 09, 2009 7:21 AM by my name

# re: New Article by Bill Wagner on Dynamic Method Bags in C# 4.0

Nice article!

Just wanted to make sure I understand:

Bill could have stored the actual lambda in the method bag, and there was no need to use expression trees, is that correct?

Daniel

Wednesday, December 23, 2009 8:55 AM by Daniel

# re: New Article by Bill Wagner on Dynamic Method Bags in C# 4.0

great, another new feature no one needs

Wednesday, December 30, 2009 5:28 AM by terraslate

# re: New Article by Bill Wagner on Dynamic Method Bags in C# 4.0

Ugh... I can already see all the brains here churning for fun, interesting, and intellectually challenging ways to use this which leads to terribly unmaintainable code.  

There's a growing trend in C# developers to follow this path rather than good basic design.  Thats why people like myself get frustrated with these types of new features.

Tuesday, January 05, 2010 6:21 AM by anon

# re: New Article by Bill Wagner on Dynamic Method Bags in C# 4.0

Anon,

I agree that this particular feature, though very useful in particular contexts, is perhaps most valuable to advanced developers. I think we have to be careful, however, about painting our opinions with too broad a brush. When taken as a whole, dynamic is a very practical feature, providing greatly enhanced access to Office and to dynamic languages such as Python and Ruby. Other recent features, such as LINQ, provide a unified query model that greatly simplifies our code and makes it much more maintainable. Overall, the recent additions to C# make it possible for developers to write code that is both more concise and easier to maintain. The primary goal of all the new features in C# is to make it possible for developers to write well designed code that adheres to the best principles of software development.

- Charlie

Tuesday, January 05, 2010 10:23 AM by Charlie Calvert

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker