博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.Net ExpandoObjecct類
阅读量:4041 次
发布时间:2019-05-24

本文共 3616 字,大约阅读时间需要 12 分钟。

今天學習了 ExpandoObjecct類. 它可以實現在運行期間動態增加或刪除屬性方法或其它成員.豐富了我原來對 dynamic對象的使用.

Namespace: System.Dynamic
Assembly: System.Linq.Expressions.dll

一. 實例化

dynamic sampleObject = new ExpandoObject();
二. 增加屬性 , 方法 , 事件
//屬性:

sampleObject.test = "Dynamic Property";Console.WriteLine(sampleObject.test);Console.WriteLine(sampleObject.test.GetType());// This code example produces the following output:// Dynamic Property// System.String

//方法: 下例使用委託增加了一個方法

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

//事件

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.

三. 作為參數

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>接口,因此我們可以對類成員進行枚舉,例如我們要枚舉成員或判斷是否包含某個成員.

dynamic employee = new ExpandoObject();employee.Name = "John Smith";employee.Age = 33;foreach (var property in (IDictionary
)employee){
Console.WriteLine(property.Key + ": " + property.Value);}// This code example produces the following output:// Name: John Smith// Age: 33

ExpandoObject 類本身沒有 delete 方法,我們可借轉化為 IDictionary 后再刪除.

dynamic employee = new ExpandoObject();employee.Name = "John Smith";((IDictionary
)employee).Remove("Name");

五. 屬性變動通知事件

ExpandoObject實例了INotifyPropertyChanged 接口,所以當增加\刪除\修改了成員, 可以設置觸發事件.

// Add "using System.ComponentModel;" line// to the beginning of the file.class Program{
static void Test() {
dynamic employee = new ExpandoObject(); ((INotifyPropertyChanged)employee).PropertyChanged += new PropertyChangedEventHandler(HandlePropertyChanges); employee.Name = "John Smith"; } private static void HandlePropertyChanges( object sender, PropertyChangedEventArgs e) {
Console.WriteLine("{0} has changed.", e.PropertyName); }}

其它詳細 可參考:

https://docs.microsoft.com/en-us/dotnet/api/system.dynamic.expandoobject?view=net-5.0

转载地址:http://spmdi.baihongyu.com/

你可能感兴趣的文章
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>
Android下调用收发短信邮件等(转载)
查看>>
Android中电池信息(Battery information)的取得
查看>>
SVN客户端命令详解
查看>>
Android/Linux 内存监视
查看>>
Linux系统信息查看
查看>>
用find命令查找最近修改过的文件
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>
android raw读取超过1M文件的方法
查看>>
ubuntu下SVN服务器安装配置
查看>>
MPMoviePlayerViewController和MPMoviePlayerController的使用
查看>>
CocoaPods实践之制作篇
查看>>
[Mac]Mac 操作系统 常见技巧
查看>>
苹果Swift编程语言入门教程【中文版】
查看>>
捕鱼忍者(ninja fishing)之游戏指南+游戏攻略+游戏体验
查看>>
iphone开发基础之objective-c学习
查看>>
iphone开发之SDK研究(待续)
查看>>
计算机网络复习要点
查看>>