Roslyn: "Microsoft.CSharp.RuntimeBinder.Binder.Convert" not found by the compiler

Created on 5 Jan 2017  路  8Comments  路  Source: dotnet/roslyn

Version Used:
Microsoft Visual Studio Enterprise 2017 RC Version 15.0.26020.0 D15REL
Microsoft .NET Framework Version 4.6.01586

Steps to Reproduce:

  1. Create a C#7 project
  2. Use the "Type" dynamic in a function or anywhere else .... it doesn't matter, e.g:
static void Main()
{
    int result(dynamic val) => (int)val;

    int a = result(7);
    int b = result(3u);
    int c = result(42L);
}
  1. Compile

Expected Behavior:
No compiler error

Actual Behavior:
The following error occures:

Error   CS0656  Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.Binder.Convert'

NOTE: Adding a reference to Microsoft.CSharp solves the issue, but it breaks existing programs/projects, as it was not needed previously (afaik)

Area-Compilers Bug Tenet-Compatibility

Most helpful comment

The compiler will generate code to bind to the dynamic objects. This is language specific as language semantics like overload resolution are kept. And because The C# and VB compilers do things differently, there's a binder for each language.

The compilers are also smart enough to know that nothing dynamic is being used and don't generate any dynamic specific code just because the dynamic keyword was used.

All 8 comments

We never permitted local functions previously, so I don't see how this code could have been broken vs a previous release.

@gafter : maybe this was indeed a bad example, my point is that dynamic does not work when used in the code without referencing to Microsoft.CSharp.dll, meaning that this following code won't compile:

static void Main()
{
    int a = 42;
    dynamic d = a;

    Console.WriteLine(d);
}

Concerning C#7-specific features:
Well it broke previous RC-C#7 projects ;)

@Unknown6656, hasn't the use of dynamic always required Microsoft.CSharp.dll?

@paulomorgado Not that I know of .... or at least I never had to reference it explicitly before ........

I will, however, check whether my VS installation is broken or not, if it is maybe missing some references on project initialization


EDIT: I checked previous projects, and I saw, that I could use dynamic without having explicitly to reference to Microsoft.CSharp.dll .... maybe the compiler was automatically including the library or something?

If you want, you can look at an older repo of mine, and you will see, that Microsoft.CSharp.dll is not mentioned in the dependencies:
https://github.com/Unknown6656/rundll.net/blob/master/RunDLL.csproj#L46
but I can use it in the code files:
https://github.com/Unknown6656/rundll.net/blob/2704d48af98cbadf3116126cfc5ede69419cf7cc/Program.cs#L1007

@Unknown6656, just because you used the dynamic keyword doesn't mean your are using the聽language feature.

All dynamic references are just object references that聽the compiler handles in a special way.

here you do have a method with a return type of dynamic.

But in the only use聽of it you store that return value in an object variable.

Your code never makes use of any dynamic features. So,聽for all intents and purpose,聽the compiler treats it as a plain object.

If you just change the type聽of that variable to dynamic:

dynamic result = FetchMethod(@class, sig.Member, sig.IsProperty, ref @static, out constructor, sig.Arguments, sig.GenericArguments);

You'll get this error:

program.cs(236,17,236,23): error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'

Only solvable by referencing Microsoft.CSharp.dll.

/cc @gafter

@paulomorgado : Right you are, Sir. I thought, that dynamic was some kind of object wrapper type only really unwrapped during run-time and not compile-time.

The compiler will generate code to bind to the dynamic objects. This is language specific as language semantics like overload resolution are kept. And because The C# and VB compilers do things differently, there's a binder for each language.

The compilers are also smart enough to know that nothing dynamic is being used and don't generate any dynamic specific code just because the dynamic keyword was used.

Well, one does learn something new every day.
Thank you very much for your support and kind explanations, Sir.

Was this page helpful?
0 / 5 - 0 ratings