有没有办法使用 C# 4 中新的协/逆变来实现这种类型层次结构?
假设我有一个像这样的句柄类:
interface IHasHandle<TObject> {
IHandle<TObject> Handle { get; }
}
interface IHandle<out TObject> {
TObject Value { get; }
}
我想使用这个类为我提供层次结构中最派生的输出类型。我现在的样子:
interface IAnimal : IHasHandle<IAnimal> { ... }
interface IMammal : IAnimal, IHasHandle<IMammal> { ... }
interface IFeline : IMammal, IHasHandle<IFeline> { ... }
class Tiger : IFeline {
IHandle<IAnimal> IHasHandle<IAnimal>.Handle { get { ... } }
IHandle<IMammal> IHasHandle<IMammal>.Handle { get { ... } }
IHandle<IFeline> IHasHandle<IFeline>.Handle { get { ... } }
public IHandle<Tiger> Handle { get { ... } }
}
这意味着当我有一个 IAnimal 时,我总是可以得到 IHandle,当我有 IMammal 时,我可以得到 IHandle 等等。
有没有人对这个结构有任何一般性评论或关于如何避免拥有的想法每种可能的实施?
Suppose I have a handle class like:
interface IHasHandle<TObject> {
IHandle<TObject> Handle { get; }
}
interface IHandle<out TObject> {
TObject Value { get; }
}
I would like to the use this class to give me the most derived output type in a hierarchy. What I have now looks like:
interface IAnimal : IHasHandle<IAnimal> { ... }
interface IMammal : IAnimal, IHasHandle<IMammal> { ... }
interface IFeline : IMammal, IHasHandle<IFeline> { ... }
class Tiger : IFeline {
IHandle<IAnimal> IHasHandle<IAnimal>.Handle { get { ... } }
IHandle<IMammal> IHasHandle<IMammal>.Handle { get { ... } }
IHandle<IFeline> IHasHandle<IFeline>.Handle { get { ... } }
public IHandle<Tiger> Handle { get { ... } }
}
This means that when I have an IAnimal, I can always get IHandle, when I have IMammal, I can get IHandle, etc.
Does anyone have any general comments on this structure or ideas for how to avoid having every possible implementation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使在 .NET 4.0 之前,也可以执行以下操作:
当然,这不会阻止您创建一些
class EvilCat : IFeline
,但它提供了一种很好的摆脱方法Tiger
中额外不需要的Handle
实现。如果您在此代码示例中将IHasHandle
泛型参数声明为out
1,则您将能够强制转换Tiger
(它实现例如,将IHasHandle
) 转换为IHasHandle
。Even before .NET 4.0 it was possible to do things like:
Of course, this won't prevent you from making some
class EvilCat : IFeline<Tiger>
, but it provides quite a good way for getting rid of an extra unneededHandle
implementations inTiger
. And if you'll declareIHasHandle
generic parameter asout
one in this sample of code, you'll be able to castTiger
(which implementsIHasHandle<Tiger>
) toIHasHandle<IMammal>
for example.