表示一个对象,该对象包含可在运行时动态添加和移除的成员
语法:
public sealed class ExpandoObject : IDynamicMetaObjectProvider, IDictionary<string, Object>, ICollection<KeyValuePair<string, Object>>, IEnumerable<KeyValuePair<string, Object>>, IEnumerable, INotifyPropertyChanged
一、创建实例
在 C# 中,要为 ExpandoObject 类的实例启用后期绑定,必须使用 dynamic 关键字。
dynamic sampleObject = new ExpandoObject();
二、添加新成员
可以向 ExpandoObject 类的实例中添加属性、方法和事件。
下面的代码示例演示如何将新属性添加到 ExpandoObject 类的实例。
sampleObject.test = "Dynamic Property"; Console.WriteLine(sampleObject.test); Console.WriteLine(sampleObject.test.GetType());
方法表示存储为委托的 lambda 表达式,可在需要时调用。 下面的代码示例演示如何添加一个递增的动态属性的值的方法。
sampleObject.number = 10; sampleObject.Increment = (Action)(() => { sampleObject.number++; }); // Before calling the Increment method. Console.WriteLine(sampleObject.number); sampleObject.Increment(); // After calling the Increment method. Console.WriteLine(sampleObject.number); // This code example produces the following output: // 10 // 11
下面的代码示例演示如何将事件添加到 ExpandoObject 类的实例。
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.
三、作为参数传递
可以将 ExpandoObject 类的实例作为参数传递。
class Program { static void Main(string[] args) { dynamic employee, manager; employee = new ExpandoObject(); employee.Name = "John Smith"; employee.Age = 33; manager = new ExpandoObject(); manager.Name = "Allison Brown"; manager.Age = 42; manager.TeamSize = 10; WritePerson(manager); WritePerson(employee); } private static void WritePerson(dynamic person) { Console.WriteLine("{0} is {1} years old.", person.Name, person.Age); // The following statement causes an exception // if you pass the employee object. // Console.WriteLine("Manages {0} people", person.TeamSize); } } // This code example produces the following output: // John Smith is 33 years old. // Allison Brown is 42 years old.
四、枚举和删除成员
ExpandoObject 类实现 IDictionary<String, Object> 接口。 这使成员枚举能够在运行时添加至 ExpandoObject 类的实例中。 如果您在编译时不知道实例可能具有的成员,这可能十分有用。
下面的代码示例演示如何将 ExpandoObject 类的实例强制转换为 IDictionary<TKey, TValue> 接口,并枚举该实例的成员。
dynamic employee = new ExpandoObject(); employee.Name = "John Smith"; employee.Age = 33; foreach (var property in (IDictionary<String, Object>)employee) { Console.WriteLine(property.Key + ": " + property.Value); } // This code example produces the following output: // Name: John Smith // Age: 33
不具有删除成员的语法的语言中 (如 C# 和 Visual Basic),可以通过将 ExpandoObject 实例隐式强制转换到 IDictionary<String, Object> 接口,然后删除作为键/值对的成员的方式来删除成员。 这将在下面的示例中显示。
dynamic employee = new ExpandoObject(); employee.Name = "John Smith"; ((IDictionary<String, Object>)employee).Remove("Name");