Dynamic in C #

Source: Internet
Author: User

Dynamic dyn = (dynamic) 1;

int j = (int) dyn;

You can see that 1 is cast to dynamic and then cast back to Int.

However, dynamic can be implicitly converted to any type, and can also be converted back from other types,

So the above code is equivalent to the following:

Dynamic dyn = 1;

int j = dyn;


The following sentence will not compile, the TypeOf operator cannot be used on a dynamic type

Console.WriteLine (typeof (Dynamic));

Console.WriteLine (typeof (List<dynamic>));

If you are typeof (dynamic) will report that the TypeOf operator cannot be used on a dynamic type error,

But if you write list<dynamic> then the output is as follows:

You can see the output as System.Object.

Microsoft's explanation for this is:

In most cases, the dynamic type behaves the same as the object type . However, the compiler does not parse or type-check for operations that contain expressions of type dynamic. The compiler packages information about the operation, and the information is later used to calculate the run-time action. In this procedure, a variable of type dynamic is compiled into a variable of type object. Therefore, the type dynamic exists only at compile time and does not exist at run time.


What dynamic is (my personal understanding) is a keyword introduced when a c#4.0, object declared with dynamic is part of an animated object, does not make type generation and inspection at compile time, makes type generation and inspection at run time, has greater flexibility
. What is "dynamic"?

generally speaking, Dynamic languages do not perform compile-time type checking and only recognize the type of objects at run time.

c# was originally created as a purely static language, but C # 4 added some dynamic elements to improve interoperability with dynamic languages and frameworks. C# The team considered a variety of design options, but eventually decided to add a new keyword to support these features: dynamic.

When we use the dynamic keyword, we tell the compiler to turn off compile-time checking. There are a number of examples of how to use this keyword on the web as well as in the MSDN documentation .

2.Dynamic, Object, var difference

keyword object Represents the System.Object type, which is the root type in the C # class hierarchy.

< Span class= "sentence" style= "margin:0px; padding:0px; " > when using When the var keyword declares a variable, the type of the variable is inferred at compile time based on the initialization string. If the compiler cannot infer the type , it generates a compilation error.

The dynamic keyword introduced in C # 4 makes it easier to write and maintain certain situations that traditionally depend on the object keyword. in fact, dynamic types use the System.Object type in the background. However, unlike object, a dynamic type does not need to perform an explicit conversion operation at compile time because it recognizes the type only at run time.

3.Dynamic General usage Examples
     
   
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. // 第一种通过as转换类型来调用类中的方法
  6. var a1 = GetAnimal() as Animal;
  7. a1.Speak();
  8. // 第二种是通过反射将object中的方法调出来
  9. a1.GetType().GetMethod("Speak").Invoke(a1, null);
  10. // 第三种调用方法是通过dynamic关键字,在运行时将object转换为Animal类,直接调用类中的方法
  11. dynamic a2 = GetAnimal();
  12. a2.Speak(); // 注意,这个dynamic是没有智能提示的,要把字母全部输出正确才行
  13. /span>type AA = assembly load ( "Dogs" gettype ( ); //dogs is the namespace name, Class1 is the class name, and the GetType method can get the type type/property of the class directly from the class name under the namespace
  14. dynamic bb = Activator.CreateInstance(aa);
  15. bb.Voice();
  16. Console . ReadKey ();
  17. }
  18. static object GetAnimal() // c#中的很多返回值都是object,通过反射才能调用其中的方法,反射太麻烦了,不如用dynamic简单,也没有dynamic性能高
  19. {
  20. return new Animal() { name = "xiaolizi Animal " };
  21. }
  22. }
  23. public class Animal
  24. {
  25. public string name = "a ben mao ";
  26. public void Speak()
  27. {
  28. Console.WriteLine("this is a Animal Speak voice");
  29. }
  30. }










From for notes (Wiz)

Dynamic in C #

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.