一个List中包含多个接口;

发布于 2024-11-02 03:53:46 字数 189 浏览 6 评论 0 原文

有没有一种方法可以在像 List 这样的泛型类中定义类型,以包含仅实现多个接口的对象?可能是类类型接口。

例如:

List<myObjectBase, IDisposable, IClonable> myList;

Is there a way to define the type in a generic class like List to have contain objects which only implent multiple interfaces? Possibly class type and interfaces.

For example:

List<myObjectBase, IDisposable, IClonable> myList;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

只想待在家 2024-11-09 03:53:46

不确定我是否理解正确,但是这样怎么样:

class MyList<T> : List<T>
  where T : myObjectBase, IDisposable, IClonable
{
}

这样您只能将对象添加到从基础派生的列表中并实现这些接口。

Not sure if I understood correctly, but how about this:

class MyList<T> : List<T>
  where T : myObjectBase, IDisposable, IClonable
{
}

This way you can only add objects to the list which derive from the base and implement those interfaces.

错爱 2024-11-09 03:53:46

不,在这种情况下,你必须用以下方式表达:

public class CommonStuff : MyObjectBase, IDisposable, IClonable {}

然后你可以写:

List<CommonStuff> myList;

No. In that case you would have to express that in the following way:

public class CommonStuff : MyObjectBase, IDisposable, IClonable {}

Then you can write:

List<CommonStuff> myList;
空‖城人不在 2024-11-09 03:53:46

不,不支持多个通用参数。

这也没有多大意义。与 ArrayList 之类的类相比,使用通用 List 类没有任何好处。您将失去所有类型安全的好处,并且最终仍然不得不将东西扔得到处都是。

更好的选择是创建一个复合类来处理您想要做的所有事情...然后使用它:

public class CommonBase : MyBaseClass, ICloneable, IDisposable
{
}

然后将其用作通用参数:

var newList = new List<CommonBase>();

No, multiple generic parameters are not supported.

It wouldn't make much sense either. There would be no benefit of using the generic List<T> class over something like an ArrayList. You would lose all of the type safety benefits and you'd wind up still having to cast things all over the place.

The better option would be to create a composite class that handles all of the things you want to do...and then use that:

public class CommonBase : MyBaseClass, ICloneable, IDisposable
{
}

And then use that as your generic parameter:

var newList = new List<CommonBase>();
与往事干杯 2024-11-09 03:53:46

一种可能有用的方法是定义一个接口 ISelf。它的一个成员 Self 只是将“this”返回为 T;然后对于任何可能组合的接口 IWhatever ,定义一个通用版本 IWhatever 。它继承了 IWhatever 和 ISelf。在这种情况下,需要实现 IFoo 的类 Whizbang。和IBar将隐式实现 ISelf、IFoo>、IBar> 等。需要同时实现 IFoo 和 IBar 的例程可以接受 IFoo 类型的参数;该参数将实现 IFoo;它的 Self 属性将实现 IBar。使用此模式实现多个接口的任何对象都可以使用以任何顺序列出的部分或全部接口转换为给定形式的嵌套接口类型。

One approach that may be helpful is to define an interface ISelf<out T> whose one member, Self, simply returns "this" as a T; then for any interface IWhatever that might be combined, define a generic version IWhatever<out T> which inherits both the IWhatever and ISelf<T>. In that case, a class Whizbang which implements IFoo<Whizbang> and IBar<Whizbang> will implicitly implement ISelf<Whizbang>, IFoo<IBar<Whizbang>>, IBar<IFoo<Whizbang>>, etc. A routine which needs something that implements both IFoo and IBar can accept a parameter of type IFoo<IBar>; that parameter will implement IFoo; its Self property will implement IBar. Any object which implements multiple interfaces using this pattern may be cast to a nested interface type of the given form using some or all of the interfaces, listed in any order.

小ぇ时光︴ 2024-11-09 03:53:46

下面是添加对我有用的多个接口的最简单的解决方案。

列表 myList = new List()

myFirstClass m1C = new myFirstClass();
mySecondClass m2C = new mySecondClass();

myList.Add(m1C);
myList.Add(m2C);

foreach (var item in myList)
{
    item.Clone();
    item.Dispose();
}

class myFirstClass : ICommonInterface  
{  
// implement interface methods  
}  

class mySecondClass : ICommonInterface  
{  
// implement interface methods  
}  


interface ICommonInterface : IDisposable, IClonable  
{  
}  


interface IDisposable  
{  
    void Dispose(){}  
}  

interface IClonable     
{  
    void Clone(){}  
}  

Below is the simplest solution for adding multiple interfaces that worked for me.

List<ICommonInterface> myList = new List<ICommonInterface>()

myFirstClass m1C = new myFirstClass();
mySecondClass m2C = new mySecondClass();

myList.Add(m1C);
myList.Add(m2C);

foreach (var item in myList)
{
    item.Clone();
    item.Dispose();
}

class myFirstClass : ICommonInterface  
{  
// implement interface methods  
}  

class mySecondClass : ICommonInterface  
{  
// implement interface methods  
}  


interface ICommonInterface : IDisposable, IClonable  
{  
}  


interface IDisposable  
{  
    void Dispose(){}  
}  

interface IClonable     
{  
    void Clone(){}  
}  
塔塔猫 2024-11-09 03:53:46

您可以使用 ArrayList 并可以检查这个列表中的一个对象 - 也许它更方便。

if(list[i] is Type)

You can use an ArrayList and can check the Type of an object in this list - maybe it is handier.

if(list[i] is Type)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文