對不起,我的標題寫得很糟糕,我是一個初學者程式員,我剛剛開始使用 ac# winforms 應用程式。在一個函式中,我創建了某種型別的物件,然后在其他函式中,我遍歷了該型別物件的串列,但是我正在切換我正在使用的控制元件型別,當我這樣做時,我必須更改型別在二十多個地方宣告我的物件。有沒有辦法創建一個保存該型別的變數,而不是從該變數定義我的所有物件,這樣我只需指定一次型別,然后更改該變數。因為我使用 winforms 控制元件作為我的型別別,所以無論我的物件是什么型別,我呼叫的所有函式都是相同的,所以我需要做的就是更改型別宣告,僅此而已,抱歉,如果這是一個愚蠢的問題任何幫助將不勝感激。
這是我的背景關系代碼片段:
private void function1(object sender, EventArgs e) //not my actual function because the real function has lots of other unrelated code
{
ListView PlaceType = new ListView(); // these ListView types i would like to replace with a placeholder if possible
ListView listview = new ListView();
int count2 = autolayoutGroups.Controls.OfType<ListView>().ToList().Count();
listview.Size = new System.Drawing.Size(150, 100);
listview.BackColor = normalColor;
listview.BorderStyle = BorderStyle.Fixed3D;
listview.ForeColor = System.Drawing.Color.Black;
listview.Name = "Group" count2;
listview.MouseDown = Select;
}
private void function2(object sender, EventArgs e)
{
List<ListView> list_of_groups = autolayoutGroups.Controls.OfType<ListView>().ToList(); // a place where I need some type of placeholder
foreach (ListView l in list_of_groups)
{
// do something here
}
}
private void function3(object sender, EventArgs e) // I have several functions like this
//and if I change the control I'm using I have to change the types in every function
{
List<ListView> list_of_groups = autolayoutGroups.Controls.OfType<ListView>().ToList(); // a place where I need some type of placeholder
foreach (ListView l in list_of_groups)
{
// do something here
}
}
uj5u.com熱心網友回復:
如果我正確理解您的問題,那么您目前有一些使用 ListView 的代碼,并且您想要相同的代碼,但是您想要一些其他類,例如 DataGridView 或 ComboBox,而不是 ListView。當然,這個其他類也必須具有您在 ListView 上使用的方法。
在 C# 中,這個概念稱為泛型。你有泛型類和泛型方法。
您可以通過鍵入識別符號而不是要用另一種型別替換的部分來定義泛型。
在您的情況下:您想用 DataGridView 替換 ListView。在函式 1 中,您創建了一個 ListView,并設定了一些屬性。首先我們將把這個創建放在一個單獨的方法中,你會得到這樣的東西:
private ListView CreateListView()
{
ListView listview = new ListView();
int count2 = autolayoutGroups.Controls.OfType<ListView>().Count();
listview.Size = new System.Drawing.Size(150, 100);
listview.BackColor = normalColor;
listview.BorderStyle = BorderStyle.Fixed3D;
listview.ForeColor = System.Drawing.Color.Black;
listview.Name = "Group" count2;
listview.MouseDown = Select;
return listView;
}
private void function1(object sender, EventArgs e)
{
ListView createdListView = this.CreateListView();
// TODO: do something with the created ListView
}
(小優化,超出問題范圍):要計算count2,不要創建所有ListViews的List,然后對它們進行計數;使用Count()上IEnumerable<ListView>。
要更改 CreateListView 使其可以創建任何型別的 TMyType,請定義如下通用方法:
private TMyType Create<TMyType>()
{
TMyType createdObject = new TMyType();
int count2 = autolayoutGroups.Controls
.OfType<TMyType>()
.Count();
createdObject.Size = new System.Drawing.Size(150, 100);
createdObject.BackColor = normalColor;
createdObject.BorderStyle = BorderStyle.Fixed3D;
createdObject.ForeColor = System.Drawing.Color.Black;
createdObject.Name = "Group" count2;
createdObject.MouseDown = Select;
return createdObject ;
}
所以我所做的就是,每當我保存時ListView,我將它替換為TMyType應該創建的型別。
用法:
ListView createdListView = this.Create<ListView>();
DataGridView createdDataGridView = this.Create<DataGridView>();
ComboBox createdComboBox = this.Create<ComboBox>();
只有一個問題。你必須告訴TMyType有一個默認的建構式(你想要做的編譯器new TMyControl()),這是具有類似的方法Size,BackColor,ForeColor,等。
如果 TMyType 是從 派生的類Control,那么您可以確定它具有所需的建構式并知道您需要使用的所有方法。
要說泛型型別是從某個型別派生而來的,請使用以下結構:
private TMyType Create<TMyType>() where TMyType: Control
{
// because you are certain the TMyType is derived from Control
// you can use all methods of class Control
}
這回答了您的問題:創建通用方法
關于泛型的其他一些事情
另一個例子:如果你想通知編譯器泛型型別實作了IComparable:
private T Process<T>(T input) where T: IComparable {...}
或多個:
private T Process<T>(T input) where T: IComparable, IEquatable {...}
最后,如果您想要求泛型型別具有默認建構式:
private T Process<T> () where T: new
uj5u.com熱心網友回復:
目前尚不清楚目標是什么,但我想您可以創建一個回傳常用串列的屬性:
private List<ListView> AutolayoutGroupControls =>
autolayoutGroups.Controls.OfType<ListView>().ToList()
然后你可以
foreach(var lv in AutolayoutGroupControls)}
...
}
但它提供的不多;如果您更改該道具以回傳其他內容,您仍然需要進行一系列更改。如果你的回圈總是做同樣的事情,把它放到一個方法中并從 N 個事件處理程式中呼叫它
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/365588.html
