我正在寫一個模擬程序,我正在尋找一種方法來存儲類似於線程函數的方法調用列表(Thread.add(Object.Method ))。然後我想刪除這些方法調用並將它們添加到線程對象。 C#中是否有內置的數據結構可以實現這一點?或者,有沒有辦法讓我做出我想做的事。C#數據結構類似於線程(存儲方法調用)
0
A
回答
3
我正在尋找一種方法來存儲方法的列表來調用
您可以創建委託列表(方法的引用),就像這樣:
public delegate void DoSomething();
public class Program
{
static void Main()
{
List<DoSomething> actions = new List<DoSomething>();
actions.Add(TestMethod);
actions.Add(delegate {
// Anonymous Method 1
});
actions.Add(delegate {
// Anonymous Method 2
});
}
static void TestMethod()
{
}
}
希望它有幫助
+0
謝謝。這正是我需要的! – 2015-04-03 02:02:22
+0
請注意,您並不需要定義新委託,因爲「Action」,「Action <...>」以及許多通常會處理此類委託的Func <...>。 – 2015-04-03 04:31:01
你可以持有'動作'委託的列表。 'List > list = new List >();' –
2015-04-03 01:21:38