background preloader

DLR

Facebook Twitter

Implementing Dynamic Interfaces. GetMetaObject() returns a new DynamicDictionaryMetaObject whenever it is called.

Implementing Dynamic Interfaces

Here’s where the first complexity enters the picture. GetMetaObject() is called every time any member of the DynamicDictionary is invoked. Call the same member 10 times, GetMetaObject() gets called 10 times. Even if your methods are statically defined in DynamicDictionary, GetMetaObject() will be called, and will intercept those methods to invoke possible dynamic behavior. Remember that dynamic objects are statically typed as dynamic, therefore have no compile time behavior defined. The DynamicMetaObject is responsible for building an Expression Tree that executes whatever code is necessary to handle the dynamic invocation. BindSetMember constructs an expression tree that will call DynamicDictionary.SetDictionaryEntry() to set a value in the dictionary. Metaprogramming quickly gets confusing, so let’s walk through this slowly. DateTime current = propertyBag2.Date = DateTime.Now; Dynamic Language Runtime - Documentation. Code Plex Project Hosting for Open Source Software Documentation Docs and specs We will update documents here more frequently than in the release zip files.

Dynamic Language Runtime - Documentation

Last edited Sep 25, 2012 at 6:46 PM by billchi , version 52 Comments jackukleja Nov 27, 2010 at 5:12 AM Would be great if all this documentation was available as Hyper Text Markup Language. 0xFDED Jun 17, 2010 at 9:33 PM The DLR documentation is excellent - some of the best I've seen. Sign in to add a comment System Requirements There are currently no defined requirements. Dynamic in C# 4.0: Creating Wrappers with DynamicObject - C# Frequently Asked Questions. The DynamicObject class enables you to override operations like getting or setting a member, calling a method, or performing any binary, unary, or type conversion operation.

Dynamic in C# 4.0: Creating Wrappers with DynamicObject - C# Frequently Asked Questions

To illustrate the issue, let’s create a very simple object that overrides the “get property” operation, so that whenever you access a property it returns the property’s name as a string. This example has no practical value. public class SampleObject : DynamicObject { public override bool TryGetMember( GetMemberBinder binder, out object result) { result = binder.Name; return true; } } As with ExpandoObject, we must use the dynamic keyword to create an instance of this class. dynamic obj = new SampleObject(); Console.WriteLine(obj.SampleProperty); //Prints "SampleProperty". The TryGetMember method returns true if the operation is successful. Now let’s move to a more complex example and create a wrapper for the XElement object.

First of all, I need to create an analog of ExpandoObject. Let’s look at the contact object. Dynamic in C# 4.0: Introducing the ExpandoObject - C# Frequently Asked Questions. Well, where else can you use this new feature? What are the use cases? Where does dynamic dispatch work better than static typing? The quick answer is that whenever you see syntax like myobject.GetProperty("Address"), you have a use case for dynamic objects.

First of all, the above syntax is difficult to read. Second, you don’t have any IntelliSense support for the property name, and if the “Address” property doesn’t exist you get a run-time exception. Here’s a code example that I took from MSDN. XElement contactXML = new XElement("Contact", new XElement("Name", "Patrick Hines"), new XElement("Phone", "206-555-0144"), new XElement("Address", new XElement("Street1", "123 Main St"), new XElement("City", "Mercer Island"), new XElement("State", "WA"), new XElement("Postal", "68042") ) ); Although LINQ to XML is a good technology and I really love it, those new XElement parts look a little bit annoying.

Just note a couple of things. Dynamic contact = new ExpandoObject(); So far, so good.