Dynamic type
Dynamic is a new feature of FrameWork4.0. The presence of dynamic makes C # an attribute of a weak language type. The compiler no longer checks the type at compile time, and the default dynamic object at compile time supports any features you want. For example, even if you know nothing about the objects returned by the Getdynamicobject method, you can invoke the code as follows, and the compiler will not complain:
Dynamic DynamicObject = Getdynamicobject ();
Console.WriteLine (Dynamicobject.name);
Console.WriteLine (Dynamicobject.samplemethod ());
When it comes to proper usage, you should first point out an error usage:
Often someone will take the keyword of Var to compare with dynamic. In fact, var and dynamic are completely two concepts and should not be compared at all. var is actually a compile-time throw to our "syntax sugar", once the Var is compiled, the compile period will automatically match the actual type of var variable, and the actual type to replace the variable's declaration, it looks as if we are coding in the actual type to declare. When dynamic is compiled, it is actually an object type, except that the compiler makes special handling of the dynamic type, and the dynamic does not perform any type checking during compilation, but instead puts the type check in the runtime.
This can be seen from the Visual Studio Editor window. Variables declared with VAR support IntelliSense because visual Studion can infer the actual type of the Var type, while variables declared with dynamic do not support IntelliSense because the compiler knows nothing about the type of its runtime. Using IntelliSense for dynamic variables prompts "This action will be resolved at run time."
The point that the dynamic variable is an object variable can be validated by the IL code, where the IL code is no longer posted. Of course, the compiler also handles dynamic declarations to distinguish between direct object variables.
Dynamic is being made up by MSDN for ease of interoperability, and I feel it is based on this that some developers misunderstand that because many developers do not touch code such as COM + or office two development, there is a compelling need for a dynamic application reason.
In daily development, I think dynamic is a valuable point: dynamic can simplify reflection.
We used reflection in this way before:
public class Dynamicsample
{
public string Name {get; set;}
public int Add (int a, int b)
{
return a + B;
}
}
Dynamicsample dynamicsample = new Dynamicsample (); Create instance I didn't use reflection to simplify the demo
var addmethod = typeof (Dynamicsample). GetMethod ("Add");
int re = (int) addmethod.invoke (dynamicsample, new object[] {1, 2});
Now, we have a simplified version of the wording:
Dynamic dynamicSample2 = new Dynamicsample ();
int Re2 = Dynamicsample2.add (1, 2);
We might disagree with this simplification, after all, it looks like the code hasn't diminished much, but if you take into account the two features of efficiency and elegance, the advantage of dynamic is apparent. The compiler optimizes dynamic and is much faster than the reflection without caching. If you want to compare, you can draw a conclusion by running 1000000 of the above code (calling the Add Method section).
Resources
Http://www.cnblogs.com/luminji/archive/2011/02/18/1957484.html