动态关键字问题

发布于 2024-10-09 16:23:00 字数 109 浏览 2 评论 0原文

请问哪个版本引入了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南街女流氓 2024-10-16 16:23:00

动态类型是在.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.

辞别 2024-10-16 16:23:00

当您使用 Visual Studio 2010 时,它默认为 C# 4.0

您不能将 C# 3.0Visual 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 to C# 4.0.

You can not use C# 3.0 with Visual Studio 2010.

Even if you target .Net Framework 3.5, it will just use Framework 3.5 and not C# 3.0.

Now, since it defaults to C# 4.0, you get to use dynamic. But for that to work, you have to reference Microsoft.CSharp.dll. That assembly is compiled with v 4.0. You can't use it under v 3.5.

dynamic needs DLR (Dynamic Language Runtime) which is not there for previous framework versions.

That is why when you try to use dynamic under Framework 3.5 project, it will freak out.

So, to summarize, to use dynamic, use Framework 4.0.

绿萝 2024-10-16 16:23:00

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.

冷夜 2024-10-16 16:23:00

只是为了知识:
这种技术称为“通过后期绑定实现多态性”,

它是在 .NET Framework 1.1 中引入的。 C# 在 4.0 版本中获得了此功能。在 Visual Basic 中,可以在没有编译错误的情况下启动它。

Public Class Foo 
   Public Sub Bar()
   End Sub
End Class

Public Class Test
   Public Sub Test()
      Dim o as Object
      o = New Foo()
      ' This will compile and work
      o.Bar()
      ' This will also compile but will throw an exception
      o.NonExistingMember()
   End Sub
End Class

`

所有的技巧都在于类型“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.

Public Class Foo 
   Public Sub Bar()
   End Sub
End Class

Public Class Test
   Public Sub Test()
      Dim o as Object
      o = New Foo()
      ' This will compile and work
      o.Bar()
      ' This will also compile but will throw an exception
      o.NonExistingMember()
   End Sub
End Class

`

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文