C # dynamicobject Dynamic objects

Source: Internet
Author: User

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());
The nature difference between dynamic and VAR keywords

var can only be used as a local variable, cannot be used for a field, a parameter, a declaration must be initialized at the same time, the type is determined at initialization time, and cannot be assigned to data of a type that cannot be implicitly type-converted.

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.

Dynamic can be used for types of fields, method parameters, method return values, type parameters for generics, etc., and can be assigned to or assigned to any type and does not require explicit coercion of type conversions, because these are executed by the runtime, thanks to the dynamic nature of the dynamics type.

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 run time.

You can see it 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.

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.
The dynamic Language runtime (Dynamic Language runtime DLR) is a new set of APIs in the. NET Framework 4 Beta 1 that provides support for the type of dynamics in C #. Dynamic programming languages like IronPython and IronRuby are also implemented.

Dynamic simplifies 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为了简化演示,我没有使用反射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);
Var,dynamic, comparison of efficiency of traditional deterministic types

Traditional type of efficiency >= var dynamic type > Dynamic type

The compiler optimizes the dynamic, which is more efficient than non-cached reflection.

Reference article:

Https://www.cnblogs.com/qiuweiguo/archive/2011/08/03/2125982.html
47296033

C # dynamicobject Dynamic objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.