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.


kick it on

Comments (14)

  1. Dave says:

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

  2. Robinson says:

    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!).

  3. says:

    this is an expression bag… not a method bag.

  4. CoryC says:

    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";

  5. ccalvert says:


    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

  6. my name says:

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

  7. Daniel says:

    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?


  8. terraslate says:

    great, another new feature no one needs

  9. anon says:

    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.

  10. ccalvert says:


    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

  11. Travis Parks says:

    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.

  12. Tammy says:

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

  13. Sylvia C. says:

    Ha, Ha! Tammy, I agree with you…..I got lost in the first three words!!!

  14. bag manufacturers says:

    Good review ! Article is informative. I gained something different………………………………………………………


Skip to main content