System.Core 4.0.0.0 System.Object System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<System.String,System.Object>> System.Collections.Generic.IDictionary<System.String,System.Object> System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<System.String,System.Object>> System.ComponentModel.INotifyPropertyChanged System.Dynamic.IDynamicMetaObjectProvider

The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members. This class supports dynamic binding, which enables you to use standard syntax like sampleObject.sampleMember instead of more complex syntax like sampleObject.GetAttribute("sampleMember").

The ExpandoObject class implements the standard Dynamic Language Runtime (DLR) interface , which enables you to share instances of the ExpandoObject class between languages that support the DLR interoperability model. For example, you can create an instance of the ExpandoObject class in C# and then pass it to an IronPython function. For more information, see Dynamic Language Runtime Overview documentation on the CodePlex Web site, and Introducing the ExpandoObject on the C# Frequently Asked Questions Web site.

The ExpandoObject class is an implementation of the dynamic object concept that enables getting, setting, and invoking members. If you want to define types that have their own dynamic dispatch semantics, use the class. If you want to define how dynamic objects participate in the interoperability protocol and manage DLR fast dynamic dispatch caching, create your own implementation of the interface.

## Creating an Instance

In C#, to enable late binding for an instance of the ExpandoObject class, you must use the dynamic keyword. For more information, see Using Type Dynamic (C# Programming Guide).

In Visual Basic, dynamic operations are supported by late binding. For more information, see Early and Late Binding.

The following code example demonstrates how to create an instance of the ExpandoObject class.

code reference: System.Dynamic.ExpandoObject#1

## Adding New Members

You can add properties, methods, and events to instances of the ExpandoObject class.

The following code example demonstrates how to add a new property to an instance of the ExpandoObject class.

code reference: System.Dynamic.ExpandoObject#2

The methods represent lambda expressions that are stored as delegates, which can be invoked when they are needed. The following code example demonstrates how to add a method that increments a value of the dynamic property.

code reference: System.Dynamic.ExpandoObject#3

The following code example demonstrates how to add an event to an instance of the ExpandoObject class.

`class Program { static void Main(string[] args) { dynamic sampleObject = new ExpandoObject(); // Create a new event and initialize it with null. sampleObject.sampleEvent = null; // Add an event handler. sampleObject.sampleEvent += new EventHandler(SampleHandler); // Raise an event for testing purposes. sampleObject.sampleEvent(sampleObject, new EventArgs()); } // Event handler. static void SampleHandler(object sender, EventArgs e) { Console.WriteLine("SampleHandler for {0} event", sender); } } // This code example produces the following output: // SampleHandler for System.Dynamic.ExpandoObject event.` `Module Module1 Sub Main() Dim sampleObject As Object = New ExpandoObject() ' Create a new event and initialize it with null. sampleObject.sampleEvent = Nothing ' Add an event handler. Dim handler As EventHandler = AddressOf SampleHandler sampleObject.sampleEvent = [Delegate].Combine(sampleObject.sampleEvent, handler) ' Raise an event for testing purposes. sampleObject.sampleEvent.Invoke(sampleObject, New EventArgs()) End Sub ' Event handler. Sub SampleHandler(ByVal sender As Object, ByVal e As EventArgs) Console.WriteLine("SampleHandler for {0} event", sender) End Sub ' This code example produces the following output: ' SampleHandler for System.Dynamic.ExpandoObject event. End Module`

## Passing As a Parameter

You can pass instances of the ExpandoObject class as parameters. Note that these instances are treated as dynamic objects in C# and late-bound objects in Visual Basic. This means that you do not have IntelliSense for object members and you do not receive compiler errors when you call non-existent members. If you call a member that does not exist, an exception occurs.

The following code example demonstrates how you can create and use a method to print the names and values of properties.

code reference: System.Dynamic.ExpandoObject#4

## Enumerating and Deleting Members

The ExpandoObject class implements the IDictionary<String, Object> interface. This enables enumeration of members added to the instance of the ExpandoObject class at run time. This can be useful if you do not know at compile time what members an instance might have.

The following code example shows how you can cast an instance of the ExpandoObject class to the interface and enumerate the instance's members.

code reference: System.Dynamic.ExpandoObject#5

In languages that do not have syntax for deleting members (such as C# and Visual Basic), you can delete a member by implicitly casting an instance of the ExpandoObject to the IDictionary<String, Object> interface and then deleting the member as a key/value pair. This is shown in the following example.

code reference: System.Dynamic.ExpandoObject#6

## Receiving Notifications of Property Changes

The ExpandoObject class implements the interface and can raise a event when a member is added, deleted, or modified. This enables ExpandoObject class integration with Windows Presentation Foundation (WPF) data binding and other environments that require notification about changes in the object content.

The following code example demonstrates how to create an event handler for the PropertyChanged event.

code reference: System.Dynamic.ExpandoObject#7

Represents an object whose members can be dynamically added and removed at run time.

- Constructor
  4.0.0.0
  To be added.

  Initializes a new ExpandoObject that does not have members.

- Method
  4.0.0.0
  System.Void `` To be added. To be added. To be added.
- Method
  4.0.0.0
  System.Void To be added. To be added.
- Method
  4.0.0.0
  System.Boolean `` To be added. To be added. To be added. To be added.
- Method
  4.0.0.0
  System.Void `` `` To be added. To be added. To be added. To be added.
- Property
  4.0.0.0
  System.Int32 To be added. To be added. To be added.
- Property
  4.0.0.0
  System.Boolean To be added. To be added. To be added.
- Method
  4.0.0.0
  System.Boolean `` To be added. To be added. To be added. To be added.
- Method
  4.0.0.0
  System.Void `` `` To be added. To be added. To be added. To be added.
- Method
  4.0.0.0
  System.Boolean `` To be added. To be added. To be added. To be added.
- Property
  4.0.0.0
  System.Object `` To be added. To be added. To be added. To be added.
- Property
  4.0.0.0
  System.Collections.Generic.ICollection<System.String> To be added. To be added. To be added.
- Method
  4.0.0.0
  System.Boolean `` To be added. To be added. To be added. To be added.
- Method
  4.0.0.0
  System.Boolean `` `` To be added. To be added. To be added. To be added. To be added.
- Property
  4.0.0.0
  System.Collections.Generic.ICollection<System.Object> To be added. To be added. To be added.
- Method
  4.0.0.0
  System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<System.String,System.Object>> To be added. To be added. To be added.
- Method
  4.0.0.0
  System.Collections.IEnumerator To be added.

  Returns an enumerator that iterates through the collection.

  An that can be used to iterate through the collection.

- Method
  4.0.0.0
  System.Dynamic.DynamicMetaObject `` To be added.

  The provided MetaObject will dispatch to the dynamic virtual methods. The object can be encapsulated inside another MetaObject to provide custom behavior for individual actions.

  The object of the type.

  The expression that represents the MetaObject to dispatch to the Dynamic virtual methods.