简体   繁体   English

如何使具有类型约束的泛型集合实现两个接口?

[英]How to make a Generic Collection with Type Constraint that implement two interfaces?

excuse me I havent dealt much with generic in c# 对不起,我在C#中对通用性的处理不多

according to this question ,how is it possible to make a generc collection that implement two interfaces i was looking for a direct way like this: of course it makes error and totally is wrong. 根据这个问题 ,怎么可能实现一个实现两个接口的generc集合,而我一直在寻找这样的直接方式: 当然,它会出错并且完全是错误的。

interface IEmployee {void DisplayInfo();}

interface ISalary {void CalculateSalary();}


class Nurse : IEmployee, ISalary
{
 //some Implementation
}


class Doctor : IEmployee, ISalary
{
 //some Implementation
}

class EntryPoint
{
 static void Main(string[] args)
  { 
  System.Collections.Generic .List<T>  employees where T: ISalary,IEmployee
   =new System.Collections.Generic .List<T>();
  }

 Nurse oNurse = new Nurse();
 Doctor oDoctor = new Doctor();

 employees.Add(oNurse);
 employees.Add(oDoctor);
}

after some Reading i found that maybe i must define a generic class like this at first: 经过一番阅读后,我发现也许我首先必须定义一个通用类:

public class HospitalEmployee<T> where T : IEmployee, ISalary

{ {

} }

and unfortunately it dosnt work ,Now I am confused and dont know what must to do exactly,please help,thank u 不幸的是它没有工作,现在我很困惑,不知道该怎么做,请帮助,谢谢

You can do it like this: 您可以这样做:

interface IEmployee { void DisplayInfo(); }
interface ISalaried { void CalculateSalary(); }
interface ISalariedEmployee : IEmployee, ISalaried {}
class Doctor : ISalariedEmployee { whatever }
class Nurse : ISalariedEmployee { whatever }
...
var list = new List<ISalariedEmployee>() { new Nurse(), new Doctor() };

Does that help? 有帮助吗?

Essentially the feature you really want does not exist. 本质上,您真正想要的功能不存在。 There is a way to say " this generic type parameter must be constructed with a type argument that implements these two interfaces " but there is, oddly enough, not a way to say " this local variable must be initialized with a reference to an object that implements these two interfaces ". 有一种说法是“ 必须使用实现这两个接口的类型实参来构造此泛型类型参数 ”,但奇怪的是,没有一种说法是“ 该局部变量必须使用对以下对象的引用进行初始化:实现这两个接口 ”。 It is simply a shortcoming of the C# type system that you can represent that in type parameters but not in locals. 您可以在类型参数中而不在本地语言中表示它,这仅仅是C#类型系统的一个缺点。 What you want is: 您想要的是:

var list = new List<IEmployee + ISalary>();

And now you can only put things into the list that implement both interfaces. 现在,您只能将实现两个接口的内容放入列表中。 But there is no such feature in C#, unfortunately. 但是不幸的是,C#中没有这样的功能。 Sorry! 抱歉!

It is not clear what are you trying to do: create your own generic container or use List<T> to store different objects. 目前尚不清楚您要做什么:创建自己的通用容器或使用List<T>存储不同的对象。

But as far as I understood you need something like this: 但据我了解,您需要这样的东西:

List<IEmployee> employees = new List<IEmployee>();
Nurse oNurse = new Nurse();
Doctor oDoctor = new Doctor();

employees.Add(oNurse);
employees.Add(oDoctor);

UPDATE 更新

Just create an interface which inherits all interfaces want to use like: 只需创建一个继承所有要使用的接口的接口即可,例如:

interface IEmployeeWithSalery: IEmployee, ISalery {}
List<IEmployeeWithSalery> employees = new List<IEmployeeWithSalery>()

This sounds a lot like my question Storing an object that implements multiple interfaces and derives from a certain base (.net) which I asked a few weeks ago. 这听起来很像我的问题,即存储一个实现多个接口的对象,该对象源自我几周前问的某个基础(.net) I offer a possible workaround there which may be more work than defining and using a few "combined" interface types, but has the advantage that one can define an object to work with any particular combination of interfaces which are suitably defined without having to define a new "combined" interface type for that combination. 我提供了一种可能的解决方法,它可能比定义和使用一些“组合”接口类型还要耗费更多精力,但是它的优点是可以定义一个对象,以与适当定义的接口的任何特定组合一起工作,而不必定义一个该组合的新的“组合”界面类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM