C# dynamic使用
在通過?dynamic?類型實現的操作中,該類型的作用是繞過編譯時類型檢查,?改為在運行時解析這些操作。?dynamic?類型簡化了對 COM API(例如 Office Automation API)、動態 API(例如 IronPython 庫)和 HTML 文檔對象模型 (DOM) 的訪問。
在大多數情況下,dynamic?類型與?object?類型的行為是一樣的。?但是,不會用編譯器對包含?dynamic?類型表達式的操作進行解析或類型檢查。?編譯器將有關該操作信息打包在一起,并且該信息以后用于計算運行時操作。?在此過程中,類型?dynamic?的變量會編譯到類型?object?的變量中。?因此,類型?dynamic?只在編譯時存在,在運行時則不存在。
以下示例將類型為?dynamic?的變量與類型為?object?的變量對比。?若要在編譯時驗證每個變量的類型,請將鼠標指針放在?WriteLine?語句中的?dyn?或?obj?上。?IntelliSense 顯示了?dyn?的“動態”和?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());
??? }
}
WriteLine?語句顯示?dyn?和?obj?的運行時類型。?此時,兩者具有相同的整數類型。?將生成以下輸出:
System.Int32
System.Int32
若要查看?dyn?和?obj?之間的差異,請在前面示例的聲明和?WriteLine?語句之間添加下列兩行之間。
dyn = dyn + 3;
obj = obj + 3;
為嘗試添加表達式?obj + 3?中的整數和對象報告編譯器錯誤。?但是,不會報告?dyn + 3?錯誤。?編譯時不會檢查包含?dyn?的表達式,原因是?dyn?的類型為?dynamic。
dynamic?關鍵字可以直接出現或作為構造類型的組件在下列情況中出現:
- · 在聲明中,作為屬性、字段、索引器、參數、返回值或類型約束的類型。?下面的類定義在幾個不同的聲明中使用?dynamic。
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;
??????? }
??? }
}
?
?
- · 在顯式類型轉換中,作為轉換的目標類型。
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
?
- · 在以類型充當值(如?is?運算符或?as?運算符右側)或者作為?typeof?的參數成為構造類型的一部分的任何上下文中。?例如,可以在下列表達式中使用?dynamic。
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));
?
?
下面的示例以多個聲明使用?dynamic。?Main?也用運行時類型檢查對比編譯時類型檢查。
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
?
?
總結
以上是生活随笔為你收集整理的C# dynamic使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows phone 应用开发[2
- 下一篇: 活动