所以,我有一個類:如何使用泛型
internal class GridBox<T> : BoxBase where T : new()
{
public GridBox(Grid grid, GridBoxView view, MessageBoxIcon icon, string caption, ObservableCollection<T> dataSource, MessageBoxButton button)
: base(grid, icon, caption, button)
{
View = view;
DataSource = dataSource;
}
public GridBoxView View { get; set; }
public ObservableCollection<T> DataSource { get; set; }
}
我用這個GridBox
Class
這裏:
public static T Show<T>(DependencyObject sender, MessageBoxIcon icon, string caption, ObservableCollection<T> dataSource, MessageBoxButton button) where T : IComparable<T>
{
Window window = Window.GetWindow(sender);
Grid grid = Extensions.FindChild<Grid>(window);
GridBoxView gridBox = new GridBoxView();
return gridBox.Show<T>(new GridBox<T>(grid, gridBox, icon, caption, dataSource, button));
}
我這裏得到一個錯誤壽在new GridBox<T>
:
'T'必須是一個具有公共無參數構造函數的非抽象類型,以便在泛型類型或方法中將其用作參數'T'
那麼,如果T來自public static T Show<T>
,我該如何使用new GridBox<T>
?
所有的答案都是正確的,但只能選擇1作爲答案,對不起。 – Krowi