nameof with generic types

前端 未结 2 1891
猫巷女王i
猫巷女王i 2020-12-20 11:33

I am trying to get the name of a method on a generic interface. I would expect this to work as the type part would be a valid typeof:

//This does not compile         


        
2条回答
  •  半阙折子戏
    2020-12-20 12:28

    This is expected. According to the documentation, your expression is disallowed, because it refers to an unbound generic type:

    Because the argument needs to be an expression syntactically, there are many things disallowed that are not useful to list. The following are worth mentioning that produce errors: predefined types (for example, int or void), nullable types (Point?), array types (Customer[,]), pointer types (Buffer*), qualified alias (A::B), and unbound generic types (Dictionary<,>), preprocessing symbols (DEBUG), and labels (loop:).

    You can work around this limitation by supplying a generic parameter:

    nameof(IGenericInterface.Method)
    
    
    

    Note: I think Microsoft should tweak nameof feature to allow references to methods of unbound generic types.

    提交回复
    热议问题