Why does "dynamic" require runtime language components?

Microsoft.CSharp is required to use the function dynamic

.
I understand that there are binders, evaluators and helpers in the assembly.
But why should it be language specific?
Why Microsoft.CSharp and not Microsoft.Dynamic or System.Dynamic?

Explain, please.
Let them say d.x

where d

dynamic

.
C # compiler 1.applies
C # language rules
2. gets "property or field access"
3. emits (figuratively) Binder.GetPropertyOrField (d, "x")
Now when asked to refer to Microsoft.CSharp one might think that a language agnostic binder can't handle this case, and C # is just something got its way through compilation and requires a dedicated library.
Did the compiler have a bad day?

+3


source to share


2 answers


To your first question, it depends on the language, because it should be.

In C #, you call a method with too many arguments and you get an error. In Javascript, additional arguments are simply ignored. In C #, you access a member that doesn't exist and gets an error, while in Javascript, you get undefined

. Even if you discovered all these different feature sets and put it all in System.Core, the next quirky language of the month will surely have some super-neat feature that it won't support. Better to be flexible.



The .NET core has common code in the System.Dynamic and System.Runtime.CompilerServices namespaces. It just can't be shared.

And as for your second question, the need for a "special C # library" can of course be removed by converting these language-related behaviors to a string, but why? This unnecessarily inflates your IL code size. This is the same reason you don't write your own Int32.Parse

every time you need to read in a number.

+2


source


One of the reasons I can think of is that Visual Basic .NET has late binding in it from day one, primarily focused on how it interacts with COM IDispatch

- so if they wanted an agnostic nexus, they would have to apply the rules Visual Basic, which include searching for member-only items Public

.

Apparently the C # designers didn't want to be that strict. You can call this class method DoStuff

from C # with a dynamic link:

public class Class1
{
    internal void DoStuff()
    {
        Console.WriteLine("Hello");
    }
}

      



While trying to call the same thing through Visual Basic Object

results MissingMemberException

in runtime.

So, since the C # designers weren't the first to come to the last side, they could either follow the example of Visual Basic, or they could say that "each language will have its own rules" - they went with the latter.

+2


source







All Articles