Why doesn't the c# compiler check “staticness” of the method at call sites with a dynamic argument?

前端 未结 2 1704
失恋的感觉
失恋的感觉 2021-02-05 05:34

Why doesn\'t the C# compiler tell me that this piece of code is invalid?

class Program
{
    static void Main(string[] args)
    {
        dynamic d = 1;
                


        
2条回答
  •  长发绾君心
    2021-02-05 06:10

    When the compiler found the operation on/with variable of type dynamic, it will emit that information using CallSite object. (The CallSite object is store information about the call.)

    In your first sample it can compile because the compiler can emit the information (e.g. type of call, method you want to call etc.). In the second code, you try to call method that doesn't exist so the compiler cannot emit IL code for you.

提交回复
热议问题