动态关键字问题
请问哪个版本引入了dynamic关键字?我在 VS2010 中发现了奇怪的行为。我将目标框架设置为 3.5。但没有编译器错误。只需将目标框架创建为 .net 3.5 的控制台应用程序并使用动态关键字即可。
pls tell me in which version dynamic keyword is introduced ? I found strange behavior in VS2010. I set target framework to 3.5. But there is no compiler error. just crate a console application with target framework to .net 3.5 and use dynamic keyword .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
动态类型是在.Net 4.0中引入的。
动态类型不是语言独有的功能(即纯粹由编译器支持)。它依赖于 DLR,这是一个需要库支持的 .Net 4.0 功能。
您不能使用动态并以 .Net 3.5 框架为目标。
The dynamic type was introduced in .Net 4.0.
The dynamic type is not a language only feature (i.e purely supported by the compiler). It relies on the DLR which is a .Net 4.0 feature which needs library support.
You cannot use dynamic and target the .Net 3.5 framework.
当您使用
Visual Studio 2010
时,它默认为C# 4.0
。您不能将
C# 3.0
与Visual Studio 2010
结合使用。即使您的目标是
.Net Framework 3.5
,它也只会使用Framework 3.5
而不是C# 3.0
。现在,由于它默认为
C# 4.0
,因此您可以使用dynamic
。但要使其工作,您必须引用 Microsoft.CSharp.dll。该程序集是使用v 4.0
编译的。您无法在v 3.5
下使用它。动态
需要DLR(动态语言运行时)
,这对于以前的框架版本来说是不存在的。这就是为什么当您尝试在
Framework 3.5
项目下使用dynamic
时,它会崩溃。因此,总而言之,要使用
动态
,请使用Framework 4.0
。When you use
Visual Studio 2010
, it defaults toC# 4.0
.You can not use
C# 3.0
withVisual Studio 2010
.Even if you target
.Net Framework 3.5
, it will just useFramework 3.5
and notC# 3.0
.Now, since it defaults to
C# 4.0
, you get to usedynamic
. But for that to work, you have to referenceMicrosoft.CSharp.dll
. That assembly is compiled withv 4.0
. You can't use it underv 3.5
.dynamic
needsDLR (Dynamic Language Runtime)
which is not there for previous framework versions.That is why when you try to use
dynamic
underFramework 3.5
project, it will freak out.So, to summarize, to use
dynamic
, useFramework 4.0
.Dynamic 关键字是作为 C# 4.0 语言的一部分引入的 - 编译器随 VS 2010 一起提供。它是一种语言功能,不需要运行时支持 (AFAIK),因此一旦兼容 C# 4.0 编译器,早期版本的运行时就不会有任何问题。在 VS 2010 中更改目标框架不会切换编译器(仍为 4.0) - 仅当您使用针对新库或运行时的功能时,我们才会收到编译器错误。例如,在 VS 2008 中,您可以对目标运行时 2.0 使用 lambda 表达式或 var 关键字,但扩展方法不可用,因为扩展属性是 3.5 程序集的一部分。
编辑:上面是错误的 - 动态关键字需要 Framework 4.0。 当目标 fx 更改为 3.5 时,我什至无法在 VS2010 中编译。我相信OP可能不会在代码中稍后使用动态var,因此编译器优化会删除它,使OP相信它的工作原理。
Dynamic keyword was introduced as part of C# 4.0 language - the compiler comes with VS 2010. Its a language feature and does not need runtime support (AFAIK) hence once complied with C# 4.0 compiler, shouldn't have any issue with earlier version of runtime. Changing target framework in VS 2010 does not switch the compiler (which remains at 4.0) - we will receive compiler error only if you use feature that targets new library or runtime. For example, in VS 2008, you could use lambda expressions or var keyword for target runtime 2.0 but extension methods were not available because extension attribute was part of 3.5 assembly.
EDIT: Above is wrong - dynamic keyword requires Framework 4.0. I couldn't even compile in VS2010 when target fx was changed to 3.5. I believe that OP might not have used the dynamic var later in the code so that compiler optimization would have removed it making OP believe that its working.
只是为了知识:
这种技术称为“通过后期绑定实现多态性”,
它是在 .NET Framework 1.1 中引入的。 C# 在 4.0 版本中获得了此功能。在 Visual Basic 中,可以在没有编译错误的情况下启动它。
`
所有的技巧都在于类型“Object”扮演了顶级父级的角色并充当动态变量
Just for the sake of the knowledge:
This technique is called 'Polymorphism through late binding'
It was introduced in .NET Framework 1.1. C# acquired this feature in version 4.0. In Visual Basic it was possible to start this withoud compilation errors.
`
All the trick is in that the type "Object" played the role of the top level parent as well as acted as a dynamic variable