Document directory
- Several dynamic implementations
- Expandoobject
- Dynamicobject
- Summary
Next, go back.
Dynamic is also an important feature of DOTNET 4.0.
Common dynamic languages, such as JavaScript, Python, and Ruby, can be dynamic. That is, their types are not checked during compilation, but are known at runtime.
Of course, dynamic operations are usually affected by IDE and efficiency.
. Net 4.0 introduces dynamic Language Runtime (DLR), bringing some feature of Dynamic Language to it.
The following is a simple study.
Type Dynamics Dynamic STR = "string ";
Console. writeline (Str. GetType (). tostring ());
The preceding example is very simple. It outputs the actual type of a dynamic object. The output is system. String.
Maybe you will think it works the same as the VaR keyword. Actually, it is not.
Most obviously, you will find that dynamic has implemented ide blocking, and its type is not speculative when it is compiled as a dynamic type.
In the preceding example, the GetType () method is interpreted as a dynamic expression in IDE (vs2010), and can be resolve only at runtime.
On the contrary, the VaR keyword is a typical syntactic sugar, which has been correctly parsed into its own real type during compilation.
For more information, see reflector.
Because of this feature, we do not have to review it as before.
// Previous method
Type type = ...;
Type. getmethod ("method name",...). Invoke (data ,...);
// Current Method
Dynamic OBJ = activator. createinstance (...);
OBJ. xxmethod ()
Several dynamic implementations
. Net 4.0 provides several dynamic types or interfaces, such as dynamicobject, expandoobject, and idynamicmetaobjectprovider.
All of them are in the system. Dynamic namespace.
Expandoobject
Expandoobject provides an extensible dynamic object. This can be expanded, including the expansion of attributes or methods on different nodes.
In the simplest example, we can use this object to implement some functions similar to those in JSON.
// JS Code
VaR test = {
Name: "test ",
FUNC: function (){
Alert ();
}
};
Similar CSHARP code is as follows:
Dynamic Test = new expandoobject ();
Test. Name = "test ";
Test. func = new action () => console. writeline ("alert" + test. Name ));
Console. writeline (test. Name );
Test. func ();
With the above features, it is very convenient to process multi-node objects (such as XML.
Dynamicobject
Generally, we define a class to inherit dynamicobject. When we use the XX method or attribute for a dynamic object, the trygetmember method can be intercepted.
The accessed method or attribute name is reflected in getmemberbinder. Name of Public Virtual bool trygetmember (getmemberbinder binder, out object result.
For example
Code Class Program
{
Static void main (string [] ARGs)
{
Dynamic Test = new testdynamicobject ();
Console. writeline (Test. Test );
Console. readkey ();
}
}
Class testdynamicobject: dynamicobject
{
Public testdynamicobject ()
{
}
Public override bool trygetmember (getmemberbinder binder, out object result)
{
String nodename = binder. Name;
Console. writeline (Binder. Name + "run ");
Result = datetime. now;
Return true;
}
}
The above code output is similar to the following:
Test Run
2010-02-25 15:15:42
Summary
Of course, DLR is not as simple as described above. It also supports some Dynamic Language binder, such as ironpython. There is related video in pdc09.
This article briefly introduces some of the capabilities of DLR.
To be continue ....