-1
我有一個函數委託返回一個對象。
我將這個對象存儲在一個列表中。
我想通過該列表來獲得對象的值進行迭代,但在for循環我不能這樣做:如何訪問委託內的對象
public static class Tracking
{
public delegate TrackingObject GetTrackObject();
public static List<GetTrackObject> TrackedLists = new List<GetTrackObject>();
public static void Register (GetTrackObject trackobject)
{
TrackedLists.Add(trackobject);
}
}
public class TrackingObject
{
object collection;
Type T;
Type K;
Type V;
public TrackingObject (object o, Type t, Type k, Type v)
{
collection = o;
T = t;
K = k;
V = v;
}
}
class TrackList<T> : List<T>
{
public string Name = null;
public int ItemCount;
public int AvgSize;
public int Size;
public TrackList (string name, byte avgsize)
{
this.Name = name;
this.AvgSize = avgsize;
this.ItemCount = this.Count;
this.Size = this.Count * this.AvgSize;
Tracking.Register(this.GetTrackObject);
}
public TrackingObject GetTrackObject()
{
TrackingObject TO = new TrackingObject(this, typeof(T), null, null);
return TO;
}
}
public static void Main()
{
foreach (Tracking.GetTrackObject method in Tracking.TrackedLists)
{ }
}
我不明白這個問題。你的錯誤是什麼?什麼是失敗? –
我必須同意@David Heffernan,這段代碼看起來很尷尬......公衆領域的背後的原因,跟蹤對象委託只是困惑我 – edvaldig