dynamic keyword problem

17,380

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Share:
17,380
Saokat Ali
Author by

Saokat Ali

Updated on June 19, 2022

Comments

  • Saokat Ali
    Saokat Ali about 2 years

    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 .

  • Saokat Ali
    Saokat Ali over 13 years
    but why there is no compiler error for 3.5 application in vs 2010
  • Tim Lloyd
    Tim Lloyd over 13 years
    The dynamic type is not a language only feature, it is supported by the DLR which is .Net 4.
  • Tim Lloyd
    Tim Lloyd over 13 years
    @saokat You must be having a brain fart - you cannot use dynamic with .Net 3.5. Please check your settings and try and compile again.
  • VinayC
    VinayC over 13 years
    @chibacity, I would have but sadly don't have VS 2010 on current machine. You are most probably right in saying that DLR dependency is needed for dynamic to work - I believed otherwise as saokat is able to use it with Fx 3.5.
  • Saokat Ali
    Saokat Ali over 13 years
    @chibacity , I know i can't use dynamic in 3.5. but try it yourself. Even i set supportedRuntime in config file. there is no runtime error.
  • Tim Lloyd
    Tim Lloyd over 13 years
    @saokat After I posted I tried myself just to make sure I was not imagining things - at least on this point I am not. I have tried it - it does not compile in 3.5.
  • Saokat Ali
    Saokat Ali over 13 years
    @chibacity , are you using vs 2010 or vs2008.
  • Tim Lloyd
    Tim Lloyd over 13 years
    @saokat I have VS2010 and VS2008. I have repeated in VS2010.
  • user541686
    user541686 over 13 years
    Like chibacity said, I don't think this works, because there's a specific library needed for dynamically invoking methods that isn't available without .NET 4.0.
  • Saokat Ali
    Saokat Ali over 13 years
    @Lambert pls check it in VS2010
  • Etienne de Martel
    Etienne de Martel over 13 years
    There is no such thing as "C# 3.5". C# 3.0 was introduced with .NET 3.5.
  • user541686
    user541686 over 13 years
    This answer seems to be incorrect... @saokat: This code: dynamic x = 10; x.ToString(); gives: Program.cs(12,4): error CS1969: One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?
  • user541686
    user541686 over 13 years
    As I stated in the accepted answer, this is the correct answer; the accepted answer is incorrect, as the compiler gives an error.
  • user541686
    user541686 over 13 years
    Why is this down-voted? The main point is that you need Microsoft.CSharp.dll, which is true. +1
  • Phill
    Phill over 13 years
    Etienne, 3.0 wasn't released as a framework like 2.0/3.5, it was released to coincide with the release of Windows Vista, since the framework wasn't 100% complete at the time, 3.0 shipped with Vista, when it was ready it was named 3.5. (AFAIK)
  • Tim Lloyd
    Tim Lloyd over 13 years
    @Phill There was never a C# 3.5.
  • Phill
    Phill over 13 years
    @chibacity - re-reads on I thought it was referring to .NET Framework, not C#. My bad.
  • VinayC
    VinayC over 13 years
    @saokat, chibacity is absolutely correct! I couldn't even compile in VS2010 when target fx was changed to 3.5. Can you share your code that works - I believe that you might not have used the dynamic var later in your code so that compiler optimization would have removed it making you believe that its working. Try to print your dynamic and you should see the problem.