阵列,列表,IEnumerable的,CustomList类投给一个迭代,并把他们 [英] Array, List, IEnumerable, CustomList class cast to one and iterate threw them

查看:111
本文介绍了阵列,列表,IEnumerable的,CustomList类投给一个迭代,并把他们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图想出一个办法,告诉我,如果某一类型是数组/列表/ IEnumerable的/集...我不关心什么样的它甚至CustomLists所以像

  FooList< T> :IList的< T>
FooList:IList的

或类似的东西。

我还挺希望一个简单的type.IsArray就足够了,但可悲的是这个心不是这样的。

我需要一种方法来检查,如果上述各类其一,然后检查的基础类型是什么,以及比它转换为基于索引的集合,在这里我可以通过入口循环。

对于一个简单的数组,这是我所需要的:

 如果(obj.GetType()。IsArray的)
{
    。变种的ElementType = obj.GetType()GetElementType();
    如果(elementType.IsPrimitive == FALSE)
    {
        VAR阵列=(阵列)OBJ;
    }
}

这应该适用于所有的集合,有可能是可能的。

编辑:

如下建议,我应该/是的IEnumerable但IEnumerable的我有我的不能设置这里面的IEnumerable某些对象的问题。

使用数组我用该方法array.SetValue(OBJ,指数),它工作正常。

当我循环扔了IE​​numerable和尝试设置一个条目是这样的:

  VAR列表= OBJ为IEnumarble;
如果(名单!= NULL)
{
    的foreach(在列表VAR项)
    {
        项目=新的对象();
    }
}

我收到以下消息:

 只读局部变量不能作为赋值目标。


解决方案

您可以尝试使用运营商投它:

  VAR枚举=列表为IEnumerable;
如果(枚举!= NULL)
{
    的foreach(在枚举对象项)
    {
        // ...
    }
}

然而,如果你需要修改它,你必须重新创建它。例如通过使用您在循环填充列表。然后重新分配给原来的变量。

或者你也可以检查类型是的IList 在首位(如数组或列表),那么你可以使用它`索引:

  VAR的IList =名单IList的;
如果(IList的!= NULL)
{
    的for(int i = 0; I< ilist.Count;我++)
    {
        IList的[I] =新价值;
    }
}

I am trying to figure out a way that tells me if a certain type is an array/list/ienumerable/collection ... I dont care what kind of it is even CustomLists so something like

FooList<T> : IList<T>
FooList : IList

or stuff like that.

I kinda hoped that a simple type.IsArray would be enough but sadly this isnt the case.

I need a way to check if its one of the above types and then check what the underlying type is, and than cast it to a Indexed based collection, where I can loop through the entries.

For a simple array this is all I need:

if (obj.GetType().IsArray)
{
    var elementType = obj.GetType().GetElementType();
    if (elementType.IsPrimitive == false)
    {
        var array = (Array)obj;
    }
}

This should work for every collection, there could possible be.

Edit:

As recommended below, I should as/is to IEnumerable but with IEnumerable I have the problem that the I cannot set certain object inside this IEnumerable.

With array I have used the method array.SetValue(obj, index) which works fine.

When I loop threw the IEnumerable and try to set one entry like this:

var list = obj as IEnumarble;
if (list != null)
{
    foreach (var item in list)
    {
        item = new object();
    }
}

I am getting the following message:

Readonly local variable cannot be used as an assignment target.

解决方案

You can try to cast it with the as operator:

var enumerable = list as IEnumerable;
if (enumerable != null)
{
    foreach (object item in enumerable)
    { 
        // ...
    }
}

However, if you need to modify it you have to recreate it. For example by using a list which you fill in the loop. Then reassign it to the original variable.

Or you could check if the type is a ILIst in the first place (like an array or list), then you can use it`s indexer:

var ilist = list as IList;
if (ilist != null)
{
    for (int i = 0; i < ilist.Count; i++)
    {
        ilist[i] = "new value";
    }
}

这篇关于阵列,列表,IEnumerable的,CustomList类投给一个迭代,并把他们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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