28

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 groupMembers in commented line. This doesn't work because it can not be converted (List<T> to List<object>). So how can I do it?


In chart the options object you could set the vAxis object with the field format and provide a string with the pattern you want to use here's an example:

new google.visualization.BarChart(document.getElementById('visualization'));
  draw(data,
       {title:"Yearly Coffee Consumption by Country",
        width:600, height:400,
        vAxis: {title: "Year", format: "yy"},
        hAxis: {title: "Cups"}}
  );

Look at the vAxis object.

For the string format you should look to http://userguide.icu-project.org/formatparse/datetime to build you the pattern you prefer.

4

5 に答える 5

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

しかし、それは良いことではないようです。元のリストとは関係のない新しい空のリストを作成しています。

これらのクラスをどのように使用するかによって、それが良いアイデアかどうかがわかります。1 つのクラスに項目を追加して両方のリストを更新する予定がある場合は、適合しません。次に、おそらくあなたDieGameActionも一般的である必要があります: DieGameAction<T>. 次に、キャストせずに元のリストを与えることができます。

しかし、別の危険があります: IGroup に新しいリストを設定すると、DieGameAction に反映されません。

したがって、それはすべて、あなたが何をしようとしているかに依存します。

于 2013-05-24T18:51:19.007 に答える
1

解決策を提供することだけに集中します。代わりに、DieGameAction に IList < object > を使用させることができます。

class DieGameAction : IGameAction {

    IList<object> gameObjectList;

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

次に、任意の IList < T > に適応する IList < object > 実装を提供できます。

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)));
    }
}

IList < T > をラップすることもできる System.Collections.ObjectModel.Collection < T > をベースとして使用して、多くの可能なソリューションの 1 つを提供しようとしています。

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();
    }
}

リストの変更により、サポートされていないオブジェクトを使用しようとすると、ここでプログラムした方法で型キャスト例外がスローされますが、好きなように処理することもできます。

ここで、 IList < object > については、代わりに List < T > によって実装されている IList を使用することもできるため、これを機能させるために基本的に何もする必要はありません。

重要なことは、基本的に同じ基になる List オブジェクトを使用するため、使用される両方の場所でリストが同じように表示されることに注意してください。

これがあなたの質問に答えているかどうか、回答としてマークするか、控えないでください:)

于 2015-04-04T19:00:40.750 に答える