Two days to see the garden there are a few articles about dynamic how to use the article, I do not understand what this dynamic is used, but let me two more questions:
Less IntelliSense when writing programs;
Slows down (reflection) when running programs.
Today after reading the new features in CSharp 4.docx just suddenly understand, while nothing, to tidy up the things, I hope to be able to temporarily do not understand what the use of dynamic friends to provide some reference. Of course, because I talents, do not guarantee that all the views are correct, I hope that we look at this article with a dialectical perspective, if there is a mistake, please criticize.
In turn, the dynamic keyword is used to declare a dynamic object and then invoke the method or read-write property through the animated object.
When using C # 2.0 or 3.0, if an object needs to be determined at run time, and there is no interface and base class information, we generally use reflection techniques to invoke this unknown pair of methods or properties, and the dynamic provided by C # 4.0 can help us simplify the work. Suppose our program gets an object of an indeterminate type at run time, but the object must have a print () method, we need to call this method to print out some information, then under C # 4.0, we can use the following two lines of code to achieve this requirement.
dynamic unknowObj = GetDymanicObject();
unknowObj.Print(); //调用动态对象的某个方法
This solution is much simpler than calling the Print method with reflection. The programmer has to do is not to make the method name print () wrong, VS2010 is not to provide smart hints for dynamic objects, because vs do not know what the runtime this unknowobj will be ...
Here, there should be a lot of friends from this example, when the program is compiled to Unknowobj.print (), VS will help us generate the reflection of the code, the reflection of the way to call the Print method, in essence, to help us automatically reflect.
If you can understand this, then it is not difficult to understand why C # to do dynamic This is neither intelligent perception, running slow monster out.
According to new features in CSharp 4, dymanic is mainly used for the following scenarios:
1, Automatic reflection
2. COM Component Interoperability
3, mixed programming, such as IronRuby and IronPython
4. Working with HTML DOM objects
If there are friends who have dealt with the above work, it should be easy to understand.
Specific content, but also ask everyone to see the new features in CSharp 4, Inside said more detailed.