ExpandoObject类

表示一个对象,该对象包含可在运行时动态添加和移除的成员

语法:

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

  

  

 

  

  

  

  

  

转载于:https://www.cnblogs.com/YanYongSong/p/4431196.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值