如何将对象转换为ArrayList或IEnumerable? [英] How do I cast an object to an ArrayList or IEnumerable?

查看:44
本文介绍了如何将对象转换为ArrayList或IEnumerable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个C#VS 2008问题...


我们的系统有2个基类,SingleEntity和NewPluralEntity。 SingleEntity提供对与操纵数据库表中的数据相关的属性和方法的访问,NewPluralEntity是我继承的通用基类,用于创建单个实体的强类型集合。


这是我们的NewPluralEntity类的声明:

展开 | 选择 | Wrap | 行号

解决方案

我很确定你不能只是"侧身"将一个茁壮成长的班级投入到不同的阶级,即使他们有相同的父母对象


你是否有理由需要一个arraylist如此糟糕?您的对象似乎支持ICollection< T>和IEnumerable< T>是的,只是像正常一样索引吗?


你可以反映对象实现的接口并将其强制转换......

展开 < span class =codeDivider> | 选择 | Wrap | Line数字


可能有一种更安全的类型,但在我编写代码示例之前,请先澄清一下。

我理解(纠正我错了)

- 你有一个通用的NewPluralEntity集合。

- 你的类有一个强类型NewPluralEntity集合的属性,这样作为具有Workplaces属性的Insured类,其是NewPluralEntity< Workplace> ;.

- 对于任何这些类,您想要检索工作集合,无论是键入的NewPluralEntity< T>集合或T对象的ArrayList。


首先,我认为你不需要通过ArrayList(是的,那个演员应该爆炸 - 泛型的方差有限,请参阅通用类型的差异),回到获得一个强类型列表。我理解,因为它是一个通用的集合,你不一定知道运行时的类型......但是......


你说你有一个GetPropertyUsingReflection获取属性名称的方法 - 因此您在运行时知道SOMETHING。


我的问题是:您的属性名称与类型之间是否存在一对一关系?

例如,在这种情况下,您是检索工作场所属性,并期望NewPluralEntity< Workplace>。所以另一个班级会有一个酒吧。 NewPluralEntity< Bar>的属性另一类有Foos字样。属性是NewPluralEntity< Foo>?


如果是,我很确定你可以通过制作GetPropertyUsingReflection(string)来做到这一点。简单地说GetNewPluralCollection< T>()。


但是如果你的设计意图是只有一个属性名称(工作场所)并且不保证类型:即工作场所可以是NewPluralEntity< Workplace>或者NewPluralEntity< Bar> ;.


可能还有一种解决方法是修改你创建包含集合的类的方法......但是一次只有一个工作...

This is a C# VS 2008 question...

Our system has 2 base classes, SingleEntity and NewPluralEntity. SingleEntity provides access to properties and methods related to manipulating data in a database table and NewPluralEntity is a generic base class that I inherit from to create strongly typed collections of single entities.

Here is the declaration of our NewPluralEntity class:

Expand|Select|Wrap|Line Numbers

解决方案

I am pretty sure you cannot just "sideways" cast one derrived class to a different derrived class, even if they have the same parent object(s)

Is there a reason you need an arraylist so bad? Your object appears to support ICollection<T> and IEnumerable<T> already, just index it like normal?


You could reflect over the interfaces the object implements and just cast it...

Expand|Select|Wrap|Line Numbers


There might be a more type safe way to do this, but before I work up the code sample, let me get some clarification.
I understand (correct me if wrong)
- You have a generic NewPluralEntity collection.
- You have classes which will have a property which is a strongly typed NewPluralEntity collection, such as a Insured class which has a Workplaces property which is a NewPluralEntity<Workplace>.
- For any of those classes, you want to retrieve a working collection, be it the typed NewPluralEntity<T> collection or an ArrayList of T objects.

First, I don''t think you need to go via ArrayList (and yes, that cast should just blow up - generics have limited variance, see Variance in Generic Types ), so lets focus back on getting a strongly typed list. I understand that because it is a generic collection, you don''t necessarily know the type at runtime...but...

You state you have a "GetPropertyUsingReflection" method which takes the property name - so you do know SOMETHING at runtime.

My question is: is there a one-to-one relation between your property names and the type?
For example, in this instance, you are retrieving the "Workplaces" property, and expect NewPluralEntity<Workplace>. So will another class have a "Bars" property that is NewPluralEntity<Bar> and another class have a "Foos" property which is NewPluralEntity<Foo>?

If yes, I am pretty sure you can do this by making "GetPropertyUsingReflection(string)" into simply GetNewPluralCollection<T>().

But tell me know before I start if your design intent is to have only one property name ("Workplaces") with no guarantee of the type: i.e. Workplaces can be NewPluralEntity<Workplace> OR NewPluralEntity<Bar>.

There may also be a workaround by modifying how you create the class which contains the collection...but one workthrough at a time...


这篇关于如何将对象转换为ArrayList或IEnumerable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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