确保编译时接口互斥?
我想确保在编译时在同一个类上永远不会找到两个接口,类似于 AttributeUsage 在编译时检查自定义属性。
例如:
[InterfaceUsage(MutuallyExclusive = typeof(B))]
interface A {
//...
}
interface B {
//...
}
class C : A, B { //should throw an error on compile time
//...
}
我显然可以在运行时通过反射来做到这一点,但我对编译时解决方案感兴趣。
我想这个可能不存在现成的 - 但有没有一种方法可以创建一个在编译时运行的自定义属性,就像 AttributeUsage 一样?
I'd like to ensure that two interfaces are never found on the same class at compile-time, similar to how AttributeUsage checks custom Attributes at compile-time.
e.g.:
[InterfaceUsage(MutuallyExclusive = typeof(B))]
interface A {
//...
}
interface B {
//...
}
class C : A, B { //should throw an error on compile time
//...
}
I can obviously do this at runtime with reflection, but I'm interested in a compile-time solution.
I'd imagine that one probably doesn't exist out of the box - but is there a way to create a custom attribute that is run at compile-time, much like AttributeUsage is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种方法是将它们更改为抽象类。
A different approach could be to change them to Abstract classes.
给他们两个具有相同签名但返回类型不兼容的方法怎么样?
How about giving them two methods with identical signatures, but incompatible return types?
一个“解决方法”是在两个接口中添加 /// ,警告由于这样那样的原因,它们不应该一起使用。它用警告信号解决了一个陷阱,但总比没有好。
我遇到了类似的情况:本地和全球条件。一个只能依赖于数组中某个位置周围的固定部分,而另一个则根本不能依赖于位置。把两者放在一起,你就得到了一个无用的条件。
A "workaround" would be to just add /// to both interfaces warning that they shouldn't be used together, because of such and such reason. It's addressing a pitfall with a caution sign, but it's better than nothing.
I ran into a similar case: Local and Global conditions. One can only depend on a fixed portion of the array around a position, while the other cannot depend on position at all. Put both together and you have a useless condition.
这可能不是 100% 的方式,但对于您想要互斥的接口,您可能会在编译时获得一些错误。假设我们想禁止人们在同一个类上实现
cat
和dog
接口:通过这种设置,如果一个类实现了,你实际上会得到一个致命的编译错误尝试公开实现两个互斥的接口,如
demo3
和demo4
中所示。这是由共享基类cat_or_dog
中成员的返回值的Type
冲突引起的:(Meow
和 < code>Woof 错误是由于 IDE 的自动接口实现功能在更严重的错误之后由于某种原因未完成重构而导致的)然而,正如您所知,这是一个奇怪的重复模板模式的变体,因此仍然有可能“错误地”实现这两个接口通过使用 C# 显式接口实现:
This may not be 100% of the way there, but you can obtain some errors at compile-time for interfaces that you want to be mutually-exclusive. Assuming we want to disallow people from implementing both of the interfaces
cat
anddog
on the same class:With this setup, you will get actually get a fatal compile error if a class tries to implement both of the mutually exclusive interfaces publically, as shown in
demo3
anddemo4
. It's caused by aType
collision on the return-value of a member in the shared base classcat_or_dog<A>
:(The
Meow
andWoof
errors are caused when the automatic interface implementation feature of the IDE not completing the refactoring after the more serious error, for some reason)As you surely know however, this is a variation of the curiously recurring template pattern, so it is still possible to "incorrectly" implement both interfaces by using C# explicit interface implementation: