The dynamic type enables the operations in which it occurs to bypass(忽略) compile-time type checking.Instead, these operations are resolved at run time.The dynamic type simplifies access to COM APIs such as the Office Automation APIs, and also to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM).
Type dynamic behaves like typeobject in most circumstances. However, operations that contain expressions of type dynamic are not resolved or type checked by the compiler.The compiler packages together information about the operation, and that information is later used to evaluate the operation at run time.As part of the process, variables of type dynamic are compiled into variables of typeobject. Therefore, type dynamic exists only at compile time, not at run time.
The following example contrasts a variable of typedynamic to a variable of type object.To verify the type of each variable at compile time, place the mouse pointer overdyn or obj in the WriteLine statements.IntelliSense shows dynamic fordyn and object for obj.
class Program { static void Main(string[] args) { dynamic dyn = 1; object obj = 1; // Rest the mouse pointer over dyn and obj to see their // types at compile time. System.Console.WriteLine(dyn.GetType()); System.Console.WriteLine(obj.GetType()); } }
The WriteLine statements display the run-time types ofdyn and obj. At that point, both have the same type, integer. The following output is produced:
System.Int32
System.Int32
To see the difference between dyn and obj at compile time, add the following two lines between the declarations and theWriteLine statements in the previous example.
dyn = dyn + 3; obj = obj + 3;
A compiler error is reported for the attempted addition of an integer and an object in expressionobj + 3. However, no error is reported fordyn + 3. The expression that containsdyn is not checked at compile time because the type of dyn is dynamic.
The dynamic keyword can appear directly or as a component of a constructed type in the following situations:
-
In declarations, as the type of a property, field, indexer, parameter, return value, local variable, or type constraint.The following class definition usesdynamic in several different declarations.
C#class ExampleClass { // A dynamic field. static dynamic field; // A dynamic property. dynamic prop { get; set; } // A dynamic return type and a dynamic paramater type. public dynamic exampleMethod(dynamic d) { // A dynamic local variable. dynamic local = "Local variable"; int two = 2; if (d is int) { return local; } else { return two; } } }
-
In explicit type conversions, as the target type of a conversion.
C#static void convertToDynamic() { dynamic d; int i = 20; d = (dynamic)i; Console.WriteLine(d); string s = "Example string."; d = (dynamic)s; Console.WriteLine(d); DateTime dt = DateTime.Today; d = (dynamic)dt; Console.WriteLine(d); } // Results: // 20 // Example string. // 2/17/2009 9:12:00 AM
-
In any context where types serve as values, such as on the right side of anis operator or an as operator, or as the argument totypeof as part of a constructed type.For example,dynamic can be used in the following expressions.
C#int i = 8; dynamic d; // With the is operator. // The dynamic type behaves like object. The following // expression returns true unless someVar has the value null. if (someVar is dynamic) { } // With the as operator. d = i as dynamic; // With typeof, as part of a constructed type. Console.WriteLine(typeof(List<dynamic>)); // The following statement causes a compiler error. //Console.WriteLine(typeof(dynamic));
The following example uses dynamic in several declarations.TheMain method also contrasts compile-time type checking with run-time type checking.
using System; namespace DynamicExamples { class Program { static void Main(string[] args) { ExampleClass ec = new ExampleClass(); Console.WriteLine(ec.exampleMethod(10)); Console.WriteLine(ec.exampleMethod("value")); // The following line causes a compiler error because exampleMethod // takes only one argument. //Console.WriteLine(ec.exampleMethod(10, 4)); dynamic dynamic_ec = new ExampleClass(); Console.WriteLine(dynamic_ec.exampleMethod(10)); // Because dynamic_ec is dynamic, the following call to exampleMethod // with two arguments does not produce an error at compile time. // However, itdoes cause a run-time error. //Console.WriteLine(dynamic_ec.exampleMethod(10, 4)); } } class ExampleClass { static dynamic field; dynamic prop { get; set; } public dynamic exampleMethod(dynamic d) { dynamic local = "Local variable"; int two = 2; if (d is int) { return local; } else { return two; } } } } // Results: // Local variable // 2 // Local variable
For more information and examples, see Using Type dynamic (C# Programming Guide).