本文共 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/