68

If I use dynamic in the immediate window of Visual Studio I get an error

Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported

How can I fix that?

Matt
  • 25,467
  • 18
  • 120
  • 187
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
  • I know your question has been asked long ago, but please check if my answer addresses the issue ... helping others having this issue too. – Matt Feb 11 '14 at 16:32

4 Answers4

119

You should add reference to Microsoft.CSharp library in the selected project or in the startup project. And your project should reference .NET Framework 4 or higher.

MSDN about immediate window context:

When establishing the context for design time expression evaluation, Visual Studio references the currently selected project in Solution Explorer. If no project is selected in Solution Explorer, Visual Studio attempts to evaluate the function against the startup project. If the function cannot be evaluated in the current context, you will receive an error message.

HoberMellow
  • 8,318
  • 2
  • 20
  • 19
  • 1
    How do I do that? I'm using the 'immediate window' – Colonel Panic Nov 26 '12 at 16:06
  • You use immediate window in environment of some project. You should add reference to this project. – HoberMellow Nov 26 '12 at 19:32
  • This is applicable also to debug watch windows. I had a solution with a startup project that did *not* reference Microsoft.CSharp, but referenced another project that *did*. Watch windows would throw the "binder not defined or imported" exception. Added a reference to Microsoft.CSharp on the **startup** project and all was well. – Snixtor Feb 05 '13 at 06:25
  • 5
    Referencing it in the currently selected project does not seem to work. I am having the same issue with System.Data, which is referenced, but cannot be found by the immediate window. Probably that only works during a debugging session? I'm using it to call a static method without starting a debugging session. – Louis Somers Aug 07 '13 at 15:43
21

Two things are important. Check the following:

  1. That your project properties are referencing .NET Framework 4 or higher (if that is not the case, change it: Right click on the project, select the "Properties..." menu item, then select as target framework ".NET Framework 4" (or higher))
  2. That you have added the "Microsoft.CSharp" assembly as reference (note that step 1., which requires to re-load the project, must be done before you can do that)

Afterwards you can use the dynamic type. The reference needs to be added in the selected project or in the startup project in order to become available in the immediate window's scope.


Note: Here I found a nice article about dynamics in C#, how it works and what you can do with it.

Matt
  • 25,467
  • 18
  • 120
  • 187
8
  1. Go to project->add reference.
  2. select assemblies from the side bar and Framework
  3. check Microsoft.CSharp

Happy coding!