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:
Dynamic DynamicObject = 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:
Type conversions
Conversions between instances of the dynamic type and other types of instances are straightforward, and developers can easily switch between dyanmic and non-dynamic behavior. Any instance can be implicitly converted to a dynamic type instance, as shown in the following example:
Dynamic D1 = 7;
Dynamic D2 = "a string";
Dynamic D3 = System.DateTime.Today;
Dynamic D4 = System.Diagnostics.Process.GetProcesses ();
Conversely, an implicit conversion can is dynamically applied to any expression of type dynamic.
Conversely, any expression of type dynamic can also be implicitly converted to another type.
int i = D1;
string str = D2;
DateTime dt = D3;
system.diagnostics.process[] procs = D4;
Overload problem with dynamic type parameter in method
If you call a method that passes an object of type dynamic, or if the object being called is of type dynamic, then the overload is judged to occur at runtime rather than at compile time.
Dynamic Language runtime (language runtime DLR)
The dynamic language runtime is a new set of APIs in the. NET Framework 4 Beta 1 that provides support for the type of dynamic in C #, as well as dynamically programming languages like IronPython and IronRuby.
Dynamic can simplify reflection.
We used to use reflection like this 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 in order to simplify the presentation, I did not use reflection var Addmethod = typeof (Dynamicsample). GetMethod ("Add"); int re = (int) addmethod.invoke (dynamicsample, new object[] {1, 2});
Now, we have a simplified notation:
Dynamic dynamicSample2 = new Dynamicsample (), int re2 = Dynamicsample2.add (1, 2);
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.