C# 将 List<T> 转换为 List<object>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16741739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:48:33  来源:igfitidea点击:

Convert List<T> to List<object>

c#

提问by wolen

I have a problem with the generic class. I have something like this:

我对泛型类有问题。我有这样的事情:

public abstract class IGroup<T> : IEnumerable where T : class {

    protected List<T> groupMembers;
    protected List<IGameAction> groupIGameActionList;

    public IGroup() {
        groupMembers = new List<T>();

        groupIGameActionList = new List<IGameAction>();
        //groupIGameActionList.Add(new DieGameAction(groupMembers));
    }
}

And second class:

第二类:

class DieGameAction : IGameAction {

    List<object> gameObjectList;

    public DieGameAction(List<object> objectList) {
        gameObjectList = objectList; 
    }
}

I don't know how to cast or convert groupMembersin commented line. This doesn't work because it can not be converted (List<T>to List<object>). So how can I do it?

我不知道如何groupMembers在注释行中进行转换或转换。这不起作用,因为它无法转换 ( List<T>to List<object>)。那么我该怎么做呢?

采纳答案by Daniel M?ller

groupMembers.Cast<object>().ToList();

But that doesn't look a good thing to do. You are creating a new empty list that will not be related to the original anymore.

但这看起来不是一件好事。您正在创建一个新的空列表,该列表不再与原始列表相关。

The way you're gonna be using these classes will tell if that would be a good idea. If you're planning to have both lists updated by adding items to a single class, it will not fit. Then maybe your DieGameActionshould be generic as well: DieGameAction<T>. Then you could give the original list without casting.

您将使用这些类的方式将说明这是否是一个好主意。如果您计划通过将项目添加到单个类来更新两个列表,则它不适合。那么也许你也DieGameAction应该是通用的:DieGameAction<T>. 然后你可以在不强制转换的情况下给出原始列表。

But, there's another danger: if you set a new list to the IGroup, it will not be reflected to DieGameAction.

但是,还有另一个危险:如果您为 IGroup 设置一个新列表,它将不会反映到 DieGameAction。

So, it all depends on what you're trying to do.

所以,这一切都取决于你想要做什么。

回答by Demetris Leptos

I'm going to focus only on providing a solution. You can make DieGameAction use IList < object > instead:

我将只专注于提供解决方案。你可以让 DieGameAction 使用 IList < object > 代替:

class DieGameAction : IGameAction {

    IList<object> gameObjectList;

    public DieGameAction(IList<object> objectList) {
        gameObjectList = objectList; 
    }
}

Then you can provide an IList < object > implementation which adapts any IList < T >.

然后你可以提供一个 IList < object > 实现,它适应任何 IList < T >。

public abstract class IGroup<T> : IEnumerable where T : class {

    protected List<T> groupMembers;
    protected List<IGameAction> groupIGameActionList;

    public IGroup() {
        groupMembers = new List<T>();

        groupIGameActionList = new List<IGameAction>();
        groupIGameActionList.Add(new DieGameAction(new ObjectListAdapter<T>(groupMembers)));
    }
}

I'm going to try and provide one of the many possible solutions using as base the System.Collections.ObjectModel.Collection < T > which can also wrap an IList < T >:

我将尝试提供许多可能的解决方案之一,使用 System.Collections.ObjectModel.Collection < T > 作为基础,它也可以包装一个 IList < T >:

public class ObjectListAdapter<T> : System.Collections.ObjectModel.Collection<T>, IList<object>
{
    public ObjectListAdapter(IList<T> wrappedList)
        : base(wrappedList)
    {
    }

    public int IndexOf(object item)
    {
        return base.IndexOf((T)item);
    }

    public void Insert(int index, object item)
    {
        base.Insert(index, (T)item);
    }

    public new object this[int index]
    {
        get
        {
            return base[index];
        }
        set
        {
            base[index] = (T)value;
        }
    }

    public void Add(object item)
    {
        base.Add((T)item);
    }

    public bool Contains(object item)
    {
        return base.Contains((T)item);
    }

    public void CopyTo(object[] array, int arrayIndex)
    {
        this.Cast<object>().ToArray().CopyTo(array, arrayIndex);
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    public bool Remove(object item)
    {
        return base.Remove((T)item);
    }

    public new IEnumerator<object> GetEnumerator()
    {
        return this.Cast<object>().GetEnumerator();
    }
}

The list changes will throw a type casting exception upon trying to use an unsupported object, the way I programmed it over here, but you can also handle that as you like.

在尝试使用不受支持的对象时,列表更改将引发类型转换异常,就像我在此处对其进行编程的方式一样,但您也可以根据需要进行处理。

Now, for IList < object > you could also try using IList instead which is also implemented by List < T > so you'll basically have to do nothing more to get this working.

现在,对于 IList < object > 你也可以尝试使用 IList 而不是它也由 List < T > 实现,所以你基本上不需要做更多的事情来让它工作。

Note that the important thing is that the list will appear the same at both places used since they will basically be using the same underlying List object.

请注意,重要的是该列表在使用的两个位置将显示相同,因为它们基本上将使用相同的底层 List 对象。

Let me know if this answers your question, by marking it as an answer, or not to refrain :)

让我知道这是否回答了您的问题,将其标记为答案,或者不要避免:)

回答by AKINNUBI ABIOLA SYLVESTER

I had similar issue today but i called the LINQ .ToArray()on it directly and it works fine. that should be shorterthan casting. so you could say

我今天遇到了类似的问题,但我LINQ .ToArray()直接调用了它,它工作正常。那应该shortercasting。所以你可以说

groupMembers.ToArray();