当从泛型类继承时,希望使用T的列表或枚举器 [英] Suggestions wanted with Lists or Enumerators of T when inheriting from generic classes

查看:119
本文介绍了当从泛型类继承时,希望使用T的列表或枚举器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道答案不会很简单,我已经使用了一些(我觉得很难看)的瑕疵。我只是寻找一些优雅的答案。



抽象类:

 公共接口IOtherObjects; 

公共抽象类MyObjects< T>其中T:IOtherObjects
{
...

public List< T> ToList()
{
...
}
}

儿童:

  public class MyObjectsA:MyObjects< OtherObjectA> //(其中,OtherObjectA实现IOtherObjects)
{


}

公共类MyObjectsB:MyObjects< OtherObjectB> //(其中,OtherObjectB实现了IOtherObjects)
{


}

是否有可能,循环遍历MyObjects(或其他类似的分组,通用或其他)的集合,然后利用 MyObjects 基类的 方法,因为我们现在还没有具体了解T的类型。

编辑
至于特定的例子,只要这个问题出现了,我已经考虑了一段时间,不同的东西,所以目前没有要求。但由于它经常出现,我想我会浮动它。



编辑
@Sara,它不是特定的类型的集合我关心,它可能是一个列表,但仍然每个实例的ToList方法是相对不可用的,没有匿名类型)

@aku,true ,这个问题可能是相对假设的,但是能够检索和处理T个对象列表,只知道它们的基类型是非常有用的。让ToList返回一个BaseType列表是我的一个解决方法



编辑 @ all:到目前为止,这已经是这种讨论我很希望,尽管它在很大程度上证实了我所怀疑的一切。感谢所有迄今为止,但任何其他人,随时输入。



编辑 @Rob,是它适用于定义的类型,但不当这个类型只被称为IOtherObjects列表。

@Rob 再次谢谢。这通常是我的迂回解决方法(不尊重:))。或者使用ConvertAll函数通过委托进行Downcast。感谢您花时间了解问题。



QUALIFYING EDIT 如果我有点困惑



更确切地说,(我可能会让我的最新实现太复杂):

可以说我拥有2个对象类型,B和C继承自对象A.



许多场景都出现在从B列表或C列表中,或者在其他情况下列出了 - 但我不知道如果我在基类中,我需要一个不太具体的A列表。上面的例子是一个淡化的例子问题列表问题的最新化身。



通常情况下,它已经呈现出来了,因为我认为通过限制代码量的可能方案这需要写作,似乎比其他选项更优雅。我真的很想讨论可能性和其他观点,我已经或多或少了。我很惊讶没有人提到ConvertAll()到目前为止,因为这是我已经使用的另一种解决方法,但有点过于详细的手头情况



@Rob 然而又和Sara



谢谢,不过我确实认为我在他们所有的静态争论中都明白泛型,并且明白了这里的问题。



我们系统的实际设计和泛型的使用(我可以说这不会带来一点偏见,因为我只是设计中的一员),做得很好。当我一直在使用核心API时,我发现了一些情况,当我在一个错误的范围内做简单的事情时,我不得不用一点不那么优雅的方式来处理它们(试图要么聪明或者懒惰 - 我会接受这些标签中的任何一个)。



我对我称之为混合物的厌恶很大程度上是因为我们需要在记录集中进行循环只是为了将对象转换为它们的基础值,这可能是一个性能问题。



我猜想我是否想知道其他人是否曾经在编码中遇到过这种情况,如果任何人在处理它的时候都比我聪明,至少比我更优雅。



  class B:A 
class C:A

您有 $ b

 列表< B>数组listB; 
列表< C> listC;

您希望将其视为父类型的列表



然后您应该使用

 列表< A> listA = listB.Cast< A>().Concat(listC.Cast< A>))。ToList()


I know the answer is not going to be simple, and I already use a couple of (I think ugly) cludges. I am simply looking for some elegant answers.

Abstract class:

public interface IOtherObjects;

public abstract class MyObjects<T> where T : IOtherObjects
{
   ...

   public List<T> ToList()
   {
       ...
   }
}

Children:

public class MyObjectsA : MyObjects<OtherObjectA> //(where OtherObjectA implements IOtherObjects)
{


}

public class MyObjectsB : MyObjects<OtherObjectB> //(where OtherObjectB implements IOtherObjects)
{


}

Is it possible, looping through a collection of MyObjects (or other similar grouping, generic or otherwise) to then utilise to ToList method of the MyObjects base class, as we do not specifically know the type of T at this point.

EDIT As for specific examples, whenever this has come up, I've thought about it for a while, and done something different instead, so there is no current requirement. but as it has come up quite frequently, I thought I would float it.

EDIT @Sara, it's not the specific type of the collection I care about, it could be a List, but still the ToList method of each instance is relatively unusable, without an anonymous type)

@aku, true, and this question may be relatively hypothetical, however being able to retrieve, and work with a list of T of objects, knowing only their base type would be very useful. Having the ToList returning a List Of BaseType has been one of my workarounds

EDIT @ all: So far, this has been the sort of discussion I was hoping for, though it largely confirms all I suspected. Thanks all so far, but anyone else, feel free to input.

EDIT@Rob, Yes it works for a defined type, but not when the type is only known as a List of IOtherObjects.

@Rob Again Thanks. That has usually been my cludgy workaround (no disrespect :) ). Either that or using the ConvertAll function to Downcast through a delegate. Thanks for taking the time to understand the problem.

QUALIFYING EDIT in case I have been a little confusing

To be more precise, (I may have let my latest implementation of this get it too complex):

lets say I have 2 object types, B and C inheriting from object A.

Many scenarios have presented themselves where, from a List of B or a List of C, or in other cases a List of either - but I don't know which if I am at a base class, I have needed a less specific List of A.

The above example was a watered-down example of the List Of Less Specific problem's latest incarnation.

Usually it has presented itself, as I think through possible scenarios that limit the amount of code that needs writing and seems a little more elegant than other options. I really wanted a discussion of possibilities and other points of view, which I have more or less got. I am surprised no one has mentioned ConvertAll() so far, as that is another workaround I have used, but a little too verbose for the scenarios at hand

@Rob Yet Again and Sara

Thanks, however I do feel I understand generics in all their static contexted glory, and did understand the issues at play here.

The actual design of our system and usage of generics it (and I can say this without only a touch of bias, as I was only one of the players in the design), has been done well. It is when I have been working with the core API, I have found situations when I have been in the wrong scope for doing something simply, instead I had to deal with them with a little less elegant than I like (trying either to be clever or perhaps lazy - I'll accept either of those labels).

My distaste for what I termed a cludge is largely that we require to do a loop through our record set simply to convert the objects to their base value which may be a performance hit.

I guess I was wondering if anyone else had come across this in their coding before, and if anyone had been cleverer, or at least more elegant, than me in dealing with it.

解决方案

If you have

class B : A
class C : A

And you have

List<B> listB;
List<C> listC;

that you wish to treat as a List of the parent type

Then you should use

List<A> listA = listB.Cast<A>().Concat(listC.Cast<A>()).ToList()

这篇关于当从泛型类继承时,希望使用T的列表或枚举器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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