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

Comments

  • Anonymous
    November 03, 2009
    The code download link is not wired up.  Looking forward to trying this out once the download is available.

  • Anonymous
    November 04, 2009
    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!).

  • Anonymous
    November 11, 2009
    this is an expression bag... not a method bag.

  • Anonymous
    November 23, 2009
    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

  • Anonymous
    November 24, 2009
    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
  • Anonymous
    December 08, 2009
    why on earth does lots fo pages published through MSDN contain javascript errors...

  • Anonymous
    December 23, 2009
    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

  • Anonymous
    December 29, 2009
    great, another new feature no one needs

  • Anonymous
    January 04, 2010
    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.

  • Anonymous
    January 05, 2010
    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
  • Anonymous
    March 12, 2010
    If C# would allow delegate types as generic arguments, this code would be more efficient, greatly simplified and extensible. Instead of storing expression trees in an Expression you could store them as Expression<TDelegate>. Instead of having a separate method for each Func/Action, you could have one. public void SetMethod<TDelegate>(string name, Expression<TDelegate> lambda) {    var desc = new Description<TDelegate> { target = lambda };    methods.Add(name, desc); } Unfortunately, lambdas cannot be converted to an Expression<TDelegate> due to the limitation that there is no way to guarantee to the compiler that TDelegate represents a delegate type. It would be nice if generic parameters could be constrained to delegate and enum types. I think this is a good example where it could go a long way. Maybe I'm wrong.

  • Anonymous
    August 19, 2010
    A little too much information for me, but it's impressive. They were'nt kidding about technology traveling at a fast rate of speed!!

  • Anonymous
    September 05, 2010
    Ha, Ha! Tammy, I agree with you.....I got lost in the first three words!!!

  • Anonymous
    December 29, 2010
    Good review ! Article is informative. I gained something different............................................................... regards: http://www.viaam.com