So, I have a class:
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; }
}
I use this GridBox
Class
here:
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));
}
I get an error here tho at the new GridBox<T>
:
'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method
So, how can I use new GridBox<T>
if the T is coming from public static T Show<T>
?
GridBox<T>
has a constraint on T
that requires the type to have a public parameterless constructor. This is what the where T : new()
specifies. (See the MSDN article on the new constraint)
As such, when you attempt to use it in your Show
method, the T
there must still satisfy this constraint. If you update your Show
method to:
public static T Show<T>(DependencyObject sender, MessageBoxIcon icon, string caption, ObservableCollection<T> dataSource, MessageBoxButton button)
where T : IComparable<T>, new()
By adding the new()
constraint to Show
, you will satisfy the constraints for GridBox
as well.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments