Original: Recommendations for improving C # programs correct usage of dynamic in 2:c#
Dynamic is a new feature of FrameWork4.0. The advent of dynamic gives C # The characteristics 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 attributes you want. For example, even if you know nothing about the objects returned by the Getdynamicobject method, you can call the code as follows, and the compiler will not error:
= Getdynamicobject ();
Console.WriteLine (Dynamicobject.name);
Console.WriteLine (Dynamicobject.samplemethod ());
When it comes to proper usage, you should first point out a wrong usage:
Often people will take the keyword Var to compare with the dynamic. In fact, var and dynamic are completely two concepts that should not be compared at all. var is actually the "syntactic sugar" that is thrown at compile time, and once compiled, the compile time automatically matches the actual type of the Var variable, and replaces the declaration of the variable with the actual type, which looks as if we were using the actual type to declare when we coded it. When dynamic is compiled, it is actually an object type, except that the compiler makes special processing of the dynamic type so that it does not perform any type checking during compilation, but instead puts type checking into the runtime.
This can be seen from the editor window of Visual Studio. Variables declared with VAR support IntelliSense because visual Studion can infer the actual type of the Var type, whereas a variable declared with dynamic does not support IntelliSense because the compiler knows nothing about the type of its run time. Using IntelliSense for dynamic variables prompts that this operation will resolve at run time.
About the dynamic variable is an object variable, which can be verified by IL code, where the IL code is no longer posted. Of course, the compiler also handles the dynamic declaration to differentiate the direct object variable.
The dynamic is being rendered by MSDN for ease of interoperability, and I feel that it is on this point that some developers misunderstand: because many developers do not touch the code such as COM +, office Two development, it is urgent to need a dynamic application justification. Well, in everyday development, I think the dynamic is valuable:
dynamic can simplify reflection .
We used to use reflection like this before:
Public classDynamicsample
{
Public stringName {Get; Set; }
Public intAdd (intA,intb)
{
returna+b;
}
}
Dynamicsample Dynamicsample= Newdynamicsample (); //Create instance in order to simplify the presentation, I did not use reflection
var addmethod= typeof(dynamicsample). GetMethod ("ADD");
intRe= (int) Addmethod.invoke (Dynamicsample,New Object[] { 1, 2 });
Now, we have a simplified notation:
=new dynamicsample ();
int= dynamicsample2.add (12);
We might dismiss this simplification, after all, it doesn't look like the code is much less, but if you take into account the two features of efficiency and elegance, then the advantages of dynamic are apparent. The compiler optimizes dynamic for a much faster reflection efficiency than without caching. If you do not want to compare, you can run the code above (call the Add Method section) 1000000 to draw a conclusion.
Improving the correct use of dynamic in the recommended 2:c# for C # programs