Convert list <T> to list <object>

I have a problem with a common 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 the second class:

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

I do not know how to distinguish or convert groupMembers to a commented line. This does not work because it cannot be converted ( List<T> to List<object> ). So how can I do this?

+6
source share
3 answers
 groupMembers.Cast<object>().ToList(); 

But this is not very good. You create a new empty list that will no longer be associated with the original.

How you are going to use these classes will tell if this is a good idea. If you plan to update both lists by adding items to the same class, it is not suitable. Then perhaps your DieGameAction should be shared: DieGameAction<T> . Then you can specify the source list without casting.

But there is another danger: if you install a new list in IGroup, this will not be reflected in DieGameAction.

So it all depends on what you are trying to do.

+14
source

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

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

You can then provide an IList <object> that adapts any 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 will try to provide one of many possible solutions using System.Collections.ObjectModel.Collection <T> as the basis, which can also wrap 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(); } } 

As a result of changing the list, a type exception will be thrown when trying to use an unsupported object, as I programmed it here, but you can also handle it as you wish.

Now, for IList <object>, you can also try using IList, which is also implemented by List <T>, so you no longer have to do anything else to make it work.

Note that the important thing is that the list will display the same in both places, as they will basically use the same underlying List object.

Let me know if this answers your question by marking it as the answer or do not refrain :)

0
source

I had a similar problem today, but I called LINQ .ToArray() on it directly, and it works great. it should be shorter than casting . so what could you say

 groupMembers.ToArray(); 
0
source

Source: https://habr.com/ru/post/945799/


All Articles