c# – 名称与泛型类型

我试图在通用接口上获取一个方法名称.我希望这可以工作,类型部分将是一个有效的类型:
//This does not compile
nameof(IGenericInterface<>.Method)

//This would compile
typeof(IGenericInterface<>)

我认为这应该是有效的C#6或者我错过了一些东西,或者他们是一个更好的方法来做到这一点.我不想使用方法名称的字符串,就好像方法重命名代码会中断而没有任何构建时间错误.

解决方法

这是预期的.根据 documentation,您的表达式是不允许的,因为它是指未绑定的通用类型:

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:).

您可以通过提供通用参数来解决此限制:

nameof(IGenericInterface<object>.Method)

注意:我认为微软应该调整name的功能,以允许引用未绑定的通用类型的方法.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...