【发布时间】:2016-10-11 19:51:06
【问题描述】:
假设我有以下扩展方法:
public static TContext Timeout<TContext>(this TContext context, TimeSpan timeout)
where TContext : IContext
{
// ...
}
这个扩展方法让我可以实现不变性并保留调用类型:
ITransactionalContext c = // ...;
c = c.Timeout(TimeSpan.FromSeconds(5));
但是,我怎样才能通过接口来实现呢?
例如,这将不一样:
public interface IContext
{
IContext Timeout(TimeSpan timeout);
}
因为我的代码示例无法编译。
ITransactionalContext c = // ...;
c = c.Timeout(TimeSpan.FromSeconds(5)); // <-- An IContext is returned. Cannot assign to variable.
我可以将接口的上下文类型指定为通用参数:
public interface IContext
{
}
public interface IContext<TContext> : IContext
where TContext : IContext
{
TContext Timeout(TimeSpan timeout);
}
public interface ITransactionalContext : IContext<ITransactionalContext>
{
}
但这似乎一点也不好看。
另外,如果ITransactionalContext 需要更多参数怎么办:
public interface ITransactionalContext<TTransaction, TEntity>
: IContext<ITransactionalContext<TTransaction, TEntity>>
{
}
这是一些泛型混乱。
有没有更简洁的方式来实现扩展方法实现的功能?
【问题讨论】:
-
“immutality”是“immutability”还是“immortality”的拼写错误?如果是后者,请立即提供完整的实现细节,谢谢。
-
Is there a cleaner way to achieve what the extension method achieves?是的,使用扩展方法。您已经有了一个完全符合您需要的解决方案,为什么还要寻找另一个解决方案? -
添加到@Servy 所说的内容,您的解决方案看起来不错,虽然是一种扩展方法,但它使用的是泛型。以这种方式使用泛型是使用继承接口的替代方法。您说您的代码示例将不一样,但是如果您有派生类,您将获得适当的多态行为。
-
我不明白你的问题。您可以在接口中声明该方法,就像您为扩展方法所做的那样:
TContext Timeout<TContext>(DateTime timeout) where TContext : IContext;。如果您只想将扩展方法移动到接口中,为什么不这样做呢?
标签: c# .net generics interface