在接口删除多余的类型 [英] removing redundant type in Interface

查看:134
本文介绍了在接口删除多余的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下接口

 公共接口IRevision< TRevisionType>
{...}

公共接口IRevisionLog
{...}

公共接口IRevisionControl<在TRevision,TLogType,T>
  其中,TRevision:IRevision< T>
  其中,TLogType:IRevisionLog
{...}
 

这code编译好,但我想知道,这是最后一吨真的需要? 当我实现IRevision我将传递一个类型T,所以实在没有必要重复的类型。

我的演示实现是:

 公共类HgRevision:IRevision<字符串>
{...}

公共类HgLog:IRevisionLog
{...}

公共类汞柱:IRevisionControl< HgRevision,HgLo​​g,串>
 

我传递字符串两次。我想知道如果我可以做这样的事情:

 公共接口IRevisionControl<在TRevision,TLogType>
  其中,TRevision:IRevision
  其中,TLogType:IRevisionLog
{...}
 

所以,我的汞是:

 公共类汞柱:IRevisionControl< HgRevision,HgLo​​g>
 

当然,这最后的片段不能编译。

解决方案
  

这code编译好,但我想知道,这是最后一吨真的需要?

是的,假设你确实需要对 TRevision 的约束。如果不这样做,那很好......但如果​​你这样做,你需要能够指定的 IRevision< T> 有有是一次转换的

一个办法是创造一个基本接口 IRevision<>

 公共接口IRevision
{
    //包括不依赖于任何TRevisionType会员
}

公共接口IRevision< TRevisionType> :IRevision
{
    // 其余的部分
}
 

然后,你可以使用:

 公共接口IRevisionControl<在TRevision,TLogType>
  其中,TRevision:IRevision
  其中,TLogType:IRevisionLog
 

...当然你那么将无法使用任何的 IRevision&LT声明的成员;> - 只有那些在非通用基本接口。

I have following interfaces

public interface IRevision<TRevisionType>
{ ... }

public interface IRevisionLog
{ ... }

public interface IRevisionControl<in TRevision, TLogType,T>
  where TRevision : IRevision<T> 
  where TLogType : IRevisionLog
{ ... }

This code compiles fine, but I am wondering, is this last one T really needed? When i implement IRevision I will be passing type T, so there is really no need to duplicate type.

My demo implementation would be:

public class HgRevision : IRevision<string>
{ ...}

public class HgLog : IRevisionLog
{ ... }

public class Hg : IRevisionControl<HgRevision, HgLog, string>

I'm passing string twice. I'm wondering if i could do anything like this:

public interface IRevisionControl<in TRevision, TLogType>
  where TRevision : IRevision
  where TLogType : IRevisionLog
{ ... }

So my Hg would be:

public class Hg : IRevisionControl<HgRevision, HgLog>

Of course, this last snippet does not compile.

解决方案

This code compiles fine, but I am wondering, is this last one T really needed?

Yes, assuming you actually need the constraint on TRevision. If you don't, that's fine... but if you do, you need to be able to specify which IRevision<T> there has to be a conversion to.

One option would be to create a base interface for IRevision<>:

public interface IRevision
{
    // Include any members which don't depend on TRevisionType
}

public interface IRevision<TRevisionType> : IRevision
{
    // The rest
}

Then you can just use:

public interface IRevisionControl<in TRevision, TLogType>
  where TRevision : IRevision 
  where TLogType : IRevisionLog

... but of course you then won't be able to use any of the members declared in IRevision<> - only the ones in the non-generic base interface.

这篇关于在接口删除多余的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆