C# 4.0 introduces a new type: dynamic, which is a static type, but an object instance of this type is able to bypass the static type checking process. In most cases, the type works like an object class. At compile time, an instance of a dynamic class supports any operation, so there is no need to worry about whether the object is taken from a COM API, or a dynamic language like IronPython, from the HTML DOM, or from reflection, or somewhere else in the program. However, if the code does something invalid, the error is caught at runtime. For example, the example method exampleMethod1 in the following code snippet has only one parameter, and the compiler can identify that it is invalid when calling ec.exampleMethod1(10,4) at compile time; This is a compilation error. The second call dynamic_ec.exampleMethod1(10,4) is not checked for static types. Therefore, the compiler does not report compilation errors. However, the bug doesn't hide forever; It will be caught at runtime and trigger a runtime exception. static void Main(string[] args){ ExampleClass ec = new ExampleClass(); dynamic dynamic_ec = new ExampleClass(); dynamic_ec.exampleMethod1(10, 4); dynamic_ec.someMethod("some argument", 7, null); dynamic_ec.nonexistentMethod();
} class ExampleClass { public ExampleClass() { } public ExampleClass(int v) { } public void exampleMethod1(int i) { } public void exampleMethod2(string str) { }
} Here's an example of using dynamic to call IronPython:
using IronPython.Hosting; using Microsoft.scrip{filtering}ting. Hosting;
Console.WriteLine("Loading random.py..."); scrip{filter}tRuntime py = Python.CreateRuntime(); dynamic random = py. UseFile("random.py"); Console.WriteLine("random.py loaded!"); var items = Enumerable.Range(1, 7). ToArray();
for (int s = 0; s < 10000; s++) { random.shuffle(items); Console.Write("Sequence {0}: ", s); foreach (int i in items) { Console.Write("{0} ", i); } Console.WriteLine();
}
For objects or expressions of type dynamic, the compiler's role is to encapsulate the actions that each expression is expected to perform, and at the time of execution, check the stored information, and any invalid statements will trigger a runtime exception. The type of result of most dynamic operations is also dynamic. For example, if you hover your mouse pointer over the testSum in the following example, the smart prompt will display a (local variable) dynamic testSum. dynamic d = 1; var testSum = d + 3; Hover the mouse pointer over the testSum below. System.Console.WriteLine(testSum);
Type conversion Transitioning between Dynamic type instances and other types of instances is simple, and developers can easily switch between dynmic and non-dynamic behaviors. Any instance can be implicitly converted to a dynamic type instance, see 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 be dynamically applied to any expression of type dynamic. And vice versa, any expression of type dynamic can also be implicitly converted to other types. int i = d1; string str = d2; DateTime dt = d3; System.Diagnostics.Process[] procs = d4;
Overload problem with dynamic type parameters in the method If a method is called passes an object of type dynamic, or the object being called is of type dynamic, then the judgment of overload occurs at runtime and not at compile time.
Dynamic language runtime DLR The dynamic language runtime is . NET Framework 4 Beta 1 is a new set of APIs that provide support for dynamic types in C# and also implement dynamic programming languages like IronPython and IronRuby.
COM interoperability C# 4.0 includes several features that improve interoperability with traditional COM API interfaces such as Office automation. Dynamic types, named parameters, and optional parameters are also part of the improvement. Many COM methods allow their parameters and return values to be of type object, so for strongly typed languages such as C#, a lot of forced type conversion is required. However, in C# 4.0, if you add the /link option at compile time, the dynamic type has a new effect: it makes the object type (parameter type or return type) in the COM interface method signature considered dynamic, thus avoiding a lot of type conversion. For example, the following statement contrasts this. It is not used dynamic. ((Excel.Range)excel. Cells[1, 1]). Value2 = "Name"; Excel.Range range = (Excel.Range)excel. Cells[1, 1];
It was a dynamic one. excel. Cells[1, 1]. Value = "Name"; Excel.Range range = excel. Cells[1, 1]; |