简体   繁体   English

类型'System.Windows.Forms.GroupBox'不能在泛型类型或方法中用作类型参数'T'

[英]The type 'System.Windows.Forms.GroupBox' cannot be used as type parameter 'T' in the generic type or method

my Class: 我的课:

public static class Global
{
    public static void TextBoxEmpty<T>(T ContainerControl) where T : ContainerControl
    {
        foreach (var t in ContainerControl.Controls.OfType<TextBox>())
        {
            t.Text = string.Empty; 
        }
    }
}

use : 采用 :

private void btnCancel_Click(object sender, EventArgs e)
{
    Global.TextBoxEmpty<GroupBox>(this.grpInfoBook);
}

error : 错误:

The type 'System.Windows.Forms.GroupBox' cannot be used as type parameter 'T' in the generic type or method 'Global.TextBoxEmpty(T)'. 类型'System.Windows.Forms.GroupBox'不能在泛型类型或方法'Global.TextBoxEmpty(T)'中用作类型参数'T'。 There is no implicit reference conversion from 'System.Windows.Forms.GroupBox' to 'System.Windows.Forms.ContainerControl'. 没有从'System.Windows.Forms.GroupBox'到'System.Windows.Forms.ContainerControl'的隐式引用转换。

What is the correct code? 什么是正确的代码?

You don't really need the where restriction at all since in the code you're using OfType to filter the list anyways. 您根本不需要where限制,因为在您使用OfType的代码中无论如何都要过滤列表。 However, if you want to keep the restriction, change it to reference System.Windows.Controls.Control : 但是,如果要保留限制,请将其更改为引用System.Windows.Controls.Control

public static class Global
{
    public static void TextBoxEmpty<T>(T ContainerControl) where T : Control
    {
        foreach (var t in ContainerControl.Controls.OfType<TextBox>())
        {
            t.Text = string.Empty; 
        }
    }
}

Take a look at the docs for GroupBox and you'll see it does not inherit from ContainerControl : 看看GroupBox的文档,你会发现它不会从ContainerControl继承:

System.Object
  System.Windows.Threading.DispatcherObject
    System.Windows.DependencyObject
      System.Windows.Media.Visual
        System.Windows.UIElement
          System.Windows.FrameworkElement
            System.Windows.Controls.Control
              System.Windows.Controls.ContentControl
                System.Windows.Controls.HeaderedContentControl
                  System.Windows.Controls.GroupBox

http://msdn.microsoft.com/en-us/library/system.windows.controls.groupbox.aspx http://msdn.microsoft.com/en-us/library/system.windows.controls.groupbox.aspx

The inheritance hierarchy of GroupBox is: GroupBox的继承层次结构是:

  System.Object 
    System.MarshalByRefObject
      System.ComponentModel.Component
        System.Windows.Forms.Control
          System.Windows.Forms.GroupBox

The type ContainerControl is not in this inheritance tree, hence the reason for the error message. ContainerControl类型不在此继承树中,因此出现错误消息的原因。

The generic constraint, you have defined restricts the usage to ContainerControl type. 您定义的泛型约束将使用限制为ContainerControl类型。 But GroupBox is not a containercontrol. 但是GroupBox不是容器控件。 It derives from Control class. 它派生自Control类。

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

相关问题 类型“ T”不能用作通用类型或方法中的类型参数“ T” - The type 'T' cannot be used as type parameter 'T' in the generic type or method 类型&#39;&#39;不能在通用类型或方法中用作类型参数&#39;T&#39; - The type '' cannot be used as type parameter 'T' in the generic type or method 类型“ XXX”不能用作通用类型或方法中的类型参数“ T” - The type 'XXX' cannot be used as type parameter 'T' in the generic type or method System.Windows.Forms.GroupBox文本丢失 - System.Windows.Forms.GroupBox text is missing System.Windows.Forms.GroupBox隐藏框架 - System.Windows.Forms.GroupBox hide frame 通用方法-类型不能用作类型参数 - Generic method - Type cannot be used as Type Parameter 当类型已知时,类型&#39;T&#39;不能用作泛型类型或方法错误中的类型参数 - The type 'T' cannot be used as type parameter in the generic type or method error when type is known 该类型不能用作通用类型或方法中的类型参数 - The type cannot be used as type parameter in the generic type or method 该类型不能用作泛型类型或方法UserStore中的类型参数TRole - The type cannot be used as type parameter TRole in the generic type or method UserStore 泛型:“相机”类型不能用作泛型类型中的类型参数“T” - Generics: The type 'Camera' cannot be used as type parameter 'T' in the generic type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM