简体   繁体   English

如何使用泛型

[英]How to use generics

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: 我在这里使用这个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));
    }

I get an error here tho at the new GridBox<T> : 我在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 'T'必须是具有公共无参数构造函数的非抽象类型,以便在泛型类型或方法中将其用作参数'T'

So, how can I use new GridBox<T> if the T is coming from public static T Show<T> ? 那么,如果T来自public static T Show<T> ,我如何使用new GridBox<T> public static T Show<T>

GridBox<T> has a constraint on T that requires the type to have a public parameterless constructor. GridBox<T>T有约束, 要求类型具有公共无参数构造函数。 This is what the where T : new() specifies. 这就是where T : new()指定的位置。 (See the MSDN article on the new constraint ) (请参阅有关新约束MSDN文章

As such, when you attempt to use it in your Show method, the T there must still satisfy this constraint. 因此,当您尝试在Show方法中使用它时, T 必须仍然满足此约束。 If you update your Show method to: 如果您将Show方法更新为:

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. 通过向Show添加new()约束,您还将满足GridBox的约束。

The generic argument of GridBox applies a constraint that you don't, which means that, if this were allowed to compile, you'd be able to pass in a type that doesn't meet its constraints. GridBox的泛型参数应用了一个您没有的约束,这意味着,如果允许编译,您将能够传入一个不符合其约束的类型。 The fix is of course simple, add the constraint to your generic argument that the type needs to have a parameterless constructor: 解决方法是当然简单,约束添加到您的泛型参数的类型需要有一个参数的构造函数:

public static T Show<T>(...) where T : IComparable<T>, new()

您需要添加new约束:

public static T Show<T>(DependencyObject sender, MessageBoxIcon icon, string caption, ObservableCollection<T> dataSource, MessageBoxButton button) where T : IComparable<T>, new()

您还需要将new()约束添加到静态函数中:

    public static T Show<T>(DependencyObject sender, MessageBoxIcon icon, string caption, ObservableCollection<T> dataSource, MessageBoxButton button) where T : IComparable<T>, new() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM