The
introduces a keyword dynamic in. Net4.0, which is a dynamically typed keyword. NET also has a keyword is Var, this is an implicit type, you can define local variables, at this time Var represents the actual data type of the compiler at the initial allocation decision, such as: Var a=1;a= "AA", the compiler will error, Because Var is already assigned to the int type when it is first defined, it cannot be used for return values, parameters, or classes/structs. This is the time to think of all types of parent object, according to the inheritance relationship, object is the parent of all types, so it can replace all classes, that is to say: Object a=1;a= "AA", such expressions, the compiler can be compiled through, But because it points to the area of memory because of the different types, so in order to properly access the type of data we need, we need to display the conversion when using it, so how much will affect performance, and the use of the time must also be very careful, accidentally forget to display the conversion will be error. Dynamic is more like a complex of Var and object, because dynamic is not a strong type, so after the compiler first defines the type, it can also define its type again based on the reassigned value, for example: Dynamic a=1;a= "AA" If you use GetType () to get its type name, you know that after assigning a string to a, the type of a becomes a string. Also because dynamic represents any type at run time (just like a variable of type object), it is necessary to use it to invoke properties, methods, and so on. Because this is a dynamic type, it is not known at run time whether the invoked dynamic variable supports the specified member, whether the parameter passed correctly has the correct spelling of the member name, so, if the data is wrong, it is not known at compile time, and the intelligence will know if there is an error while running. At the same time, VS also does not provide IntelliSense at development time. Thus, we need to be very careful when using variables defined by dynamic, and we can use Try...catch to make the program more elegant. When dynamic is faulted, the error that is thrown is usually the Runtimebinderexception class.
The scope of dynamic is not only available for variables, but also for parameters, return values, classes/structs, but dynamic cannot use lambda expressions and C # Anonymous methods, variables declared with dynamic cannot be used with LINQ to object and other LINQ techniques. Although there are some flaws in dynamic, using dynamic on some occasions does save us a lot of effort, such as late binding when using assemblies through reflection. For example, we now have a carlibrary assembly that needs to be loaded with reflection, using the Turboboost (int a,int b) method of the Minivan class in which the code is:
Assembly asm=assembly.load ("carlibrary");//Loading assemblies
Type type=asm. GetType ("carlibrary."); /get Meta data for minivan type
Object Obj=activator.createinstance (type);//Create Minivan type
MethodInfo Mi=math. GetMethod ("Turboboost");//Get information about methods
Object[] args={10,20};
Oil Invoke (Obj,args);//Call method
If you use dynamic, the code is as follows:
Assembly asm=assembly.load ("carlibrary");//Loading assemblies
Type type=asm. GetType ("carlibrary."); /get Meta data for minivan type
Dynamic obj=activator.createinstance (type);
Obj. Turboboost (10,20);
The convenience of using dynamic is visible from above.
Proficient in C # 16th-dynamic type and dynamic Language runtime-sections I through fourth