tailieunhanh - Lập trình .net 4.0 và visual studio 2010 part 8
IDynamicMetaObjectProvider là một giao diện quan trọng trong thế giới năng động, đại diện cho một đối tượng có hoạt động bị ràng buộc khi chạy. Cả hai ExpandoObject và DynamicObject thực hiện giao diện này. Bạn có thể sử dụng giao diện này để thêm tính năng năng động đến các lớp học của riêng bạn. | CHAPTER 3 LANGUAGE AND DYNAMIC CHANGES public override bool TryGetMember GetMemberBinder binder out object result string nodeName result test .Element nodeName .Value return true In this example the TryGetMember method intercepts the call to .nodel and .node2 thus allowing us to query the XML document and return the individual nodes. IDynamicMetaObjectProvider IDynamicMetaObjectProvider is an important interface in the dynamic world that represents an object that has operations bound at runtime. Both ExpandoObject and Dynamicobject implement this interface. You can use this interface to add dynamic functionality to your own classes. IDynamicMetaObjectProvider requires you to implement GetMetaObject which resolves binding operations for example method or property invocation on your object . Dynamic Limitations When working with dynamic objects there are a number of constraints you should be aware of All methods and properties in classes have to be declared public to be dynamically accessible. You cannot use the DLR to create classes in C or . Apparently the DLR does allow you to create classes but this cannot be expressed using C or . Dynamic objects cannot be passed as arguments to other functions. Extension methods cannot be called on a dynamic object and a dynamic object cannot be passed into extension objects. Annoyingly these restrictions stop you calling an extension method on a dynamic object. Dynamic IL You may be wondering what code the C compiler generates when you use the dynamic keyword. Let s take a look at the IL that is generated using ILDASM for a simple console application that declares and initializes a string string d d What do I look like in IL 55 CHAPTER 3 LANGUAGE AND DYNAMIC CHANGES This will generate the following IL .method private hidebysig static void Main string args cil managed .entrypoint Code size 8 0x8 .maxstack 1 .locals init 0 string d IL_0000 nop IL_0001 Idstr What do I look like in IL IL_0006 .
đang nạp các trang xem trước