Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the practical use of "dynamic" variable in C# 4.0?

Tags:

c#

.net

c#-4.0

What is their use if when you call the method, it might not exist?

Does that mean that you would be able to dynamically create a method on a dynamic object?

What are the practical use of this?

like image 793
Maxime Rouiller Avatar asked Oct 31 '08 13:10

Maxime Rouiller


1 Answers

You won't really be able to dynamically create the method - but you can get an implementation of IDynamicMetaObject (often by extending DynamicObject) to respond as if the method existed.

Uses:

  • Programming against COM objects with a weak API (e.g. office)
  • Calling into dynamic languages such as Ruby/Python
  • Potentially making "explorable" objects - imagine an XPath-like query but via a method/property calls e.g. document.RootElement.Person[5].Name["Attribute"]
  • No doubt many more we have yet to think of :)
like image 160
Jon Skeet Avatar answered Sep 28 '22 03:09

Jon Skeet