我试图在通用接口上获取一个方法的名称.我希望这可以工作,类型部分将是一个有效的类型:
//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
orvoid
),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)