c#接口和通用类型 [英] c# Interfaces and Generic Types

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

问题描述





我正在尝试创建一个通用接口。只是一个简单的开始:



  interface  IFactory< T> 其中 T:IModel 
{
T Get< T>( int id) ;
}



当我实现这个界面时,我希望它符合这个:

  public   class  MemberFactory:IFactory< Member> 
{
会员获取( int id)
{

}
}



但我得到的是:

  public   class  MemberFactory:IFactory< Member> 
{
T Get< T>( int id)
{

}
}





我查看对象库中的示例(即IList< member>),它确实正确实现,但是我猜测有一些我缺少的元信息?



任何帮助都会很棒。我试图搜索t'internets,但要么我的googlefu不强,或搜索条件导致太多的基本教程。无论如何 - 我会停止胡扯。



谢谢^ _ ^

解决方案

在你的IFactory< t> ;接口泛型类型参数在接口级别指定。但是,您的Get< t>方法还指定了类型T的通用参数......这可能是问题所在。类型参数T对整个接口有效,可以视为类型。你不需要你的Get()方法也有一个类型参数,并且可以强制在你的界面中没有它的返回类型T。



一次完成后,实现界面将完全按照您的意愿。



  public   interface  IFactory< T> 其中 T:IModel 
{
T Get( int id);
}

public class 成员:IModel
{
}

public class MemberFactory:IFactory< Member>
{
public 成员获取( int id)
{
throw new NotImplementedException();
}
}


解决方案1解释了使用隐式实现的接口实现,其中 public 修饰符基本上是必需的(你错过了)。但是,如果您真的需要以其他方式使用此成员,这不仅仅是通过界面(在大多数情况下最好避免使用)。



在其他情况下,最好使用接口的显式实现。它使得对已实现成员的访问只抛出接口引用,这更加清晰。方法如下:

  public   class 成员:IModel { / *   ... * / } 

public class MemberFactory:IFactory< Member>
{
会员IFactory<会员> .Get( int id)
{
// ...
}
}





我还想添加 MemberFactory 本身可以更好地通用。如果某些功能取决于特定的泛型类型,则可以实现所有常用功能(通用参数通用),使其成为抽象,并覆盖派生类中的细节。具体如下:

  public   interface  IModel {}; 
public interface IFactory< Member> 其中成员:IModel {Member Get( int id); }

public class 成员:IModel { / * ... * / }

public class MemberFactory< Member> :IFactory<会员> 其中成员:IModel {
成员IFactory< Member> .Get( int id){
return 默认(会员); // 替换为真实代码
}
// overide是用于创建专业化的派生类:
protected abstract void Something(会员);
}







-SA

Hi,

I am trying to create a generic interface. Just a simple one to start:

interface IFactory<T> where T: IModel
{
    T Get<T>(int id);
}


When I implement this interface I would like it to match this:

public class MemberFactory : IFactory<Member>
{
    Member Get(int id)
    {

    }
}


but all i get is:

public class MemberFactory : IFactory<Member>
{
    T Get<T>(int id)
    {

    }
}



I looked through the object library for examples (i.e. IList<member>) which does implement correctly, but I'm guessing that there is some meta information I'm missing?

Any help would be great. I tried to search t'internets but either my googlefu is not strong or the search terms lead to far too many basic tutorials. Anyway - I'll stop waffling.

Thanks ^_^

解决方案

In your IFactory<t> interface the generic type parameter is specified at the interface level. However, your Get<t> method also specifies a generic parameter of type T... This is likely the problem. the type parameter T is valid for the whole interface and can be treated as a type. You don't need your Get() method to have a type parameter as well and can force a return type of "T" without it in your interface.

Once done, implementing the interface will be exactly as you desire.

public interface IFactory<T> where T: IModel
{
    T Get(int id);
}

public class Member : IModel
{
}

public class MemberFactory : IFactory<Member>
{
    public Member Get(int id)
    {
        throw new NotImplementedException();  
    }
}


Solution 1 explains the implementation of interface using implicit implementation where public modifier is essentially required (which you missed). However, this is only good if you really need to use this members in some other ways, not only through the interface (and it's best to avoid, in most cases).

In other cases, it's better to use the explicit implementation of the interface. It gives the access to the implemented member only throw the interface reference, which is clearer. Here is how:

public class Member : IModel { /* ... */ }

public class MemberFactory : IFactory<Member>
{
    Member IFactory<Member>.Get(int id)
    {
        //...
    }
}



I also want to add that MemberFactory itself can better be generic. If some functionality depends on the specific generic types, you can implement all common functionality (common to generic parameters), make it abstract, and override the specifics in derived classes. This is how:

public interface IModel { };
public interface IFactory<Member> where Member: IModel { Member Get(int id); }

public class Member : IModel { /* ... */ }

public class MemberFactory<Member> : IFactory<Member> where Member: IModel {
    Member IFactory<Member>.Get(int id) {
        return default(Member); //replace with real code
    }
    //overide is derived classes to create specialization:
    protected abstract void Something(Member member);
}




—SA


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

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