dynamic 类型的作用是绕过编译时类型检查,改为在运行时进行解析。 dynamic 类型简化了对 COM API(例如 Office Automation API)、动态 API(例如 IronPython 库)和 HTML 文档对象模型 (DOM) 的访问。

在大多数情况下,dynamic 类型与 object 类型的行为类似。 但是,如果操作包含 dynamic 类型的表达式,那么不会通过编译器对该操作进行解析或类型检查。 编译器将有关该操作信息打包在一起,之后这些信息会用于在运行时评估操作。 在此过程中,dynamic类型的变量会编译为 object 类型的变量。 因此,dynamic 类型只在编译时存在,在运行时则不存在。

class Program
{
static void Main(string[] args)
{
dynamic dyn = ;
object obj = ; // 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());
}
}

上下文

dynamic 关键字可以直接出现,也可以作为构造类型的组件在下列情况中出现:

  • 在声明中,作为属性、字段、索引器、参数、返回值、本地变量或类型约束的类型。 下面的类定义在多个不同的声明中使用 dynamic
class ExampleClass
{
// A dynamic field.
static dynamic field; // A dynamic property.
dynamic prop { get; set; } // A dynamic return type and a dynamic parameter type.
public dynamic exampleMethod(dynamic d)
{
// A dynamic local variable.
dynamic local = "Local variable";
int two = ; if (d is int)
{
return local;
}
else
{
return two;
}
}
}
  • 在显式类型转换中,作为转换的目标类型。
static void convertToDynamic()
{
dynamic d;
int i = ;
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.
// 7/25/2018 12:00:00 AM
  • 在以下任何情况下:类型用作值(如 is 运算符或 as 运算符右侧),或者用作构造类型中 typeof 的参数。 例如,可以在下列表达式中使用 dynamic
int i = ;
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());
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()); // 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 = ; if (d is int)
{
return local;
}
else
{
return two;
}
}
}
}
// Results:
// Local variable
// 2
// Local variable

最新文章

  1. [工作中的设计模式]享元模式模式FlyWeight
  2. 手把手教你用python抓网页数据
  3. (Struts)ActionForm类及表单数据验证
  4. spark小技巧-mapPartitions
  5. R语言实战读书笔记(一)R语言介绍
  6. C#_ajax fileupload
  7. EDM排版table设置padding在ie7下bug
  8. matplotlib 显示中文
  9. 百度地图V2.0实践项目开发工具类bmap.util.js V1.4
  10. 5分钟看懂svg path 路径的所有命令(更有API解释、有图、有图文对比解析)
  11. Alpha第一天
  12. LeetCode Binary Search Summary 二分搜索法小结
  13. STL:vector容器用法详解
  14. 从零开始学习和改造activiti流程引擎的13天,自己记录一下
  15. Spark机器学习解析下集
  16. 20162322 朱娅霖 作业005&amp;006 栈,队列
  17. NTRIP协议学习(一)
  18. hdu6057 Kanade&#39;s convolution 【FWT】
  19. Linux下串口操作之数据拼接
  20. Mac 下安装Java

热门文章

  1. jQuery属性和样式操作
  2. sql 使用汇总(PQSQL)
  3. freebsd安装ports
  4. codevs 1553 互斥的数
  5. 如何计算CDS view里两个时间戳之间的天数间隔
  6. scrollviews page分页实现方式
  7. Assertion failure layoutSublayersOfLayer:], /SourceCache
  8. 多维的vector定义和初始化
  9. 在Phonegap下实现oAuth认证
  10. cocos2d popSceneWithTransition()方法