简体   繁体   English

C#-如何在“通用类”类型的MainForm类中声明变量而不指定通用类型

[英]C# - How can I declare a variable in MainForm class of type “Generic Class” without specifing generic type

I have the following generic class: 我有以下通用类:

internal class AutoRegisterThread<T> where T: AutoRegisterAbstract
{
field1....
method1...
}

I have 5 classes that implement AutoRegisterAbstract (abstract class). 我有5个实现AutoRegisterAbstract的类(抽象类)。

and in my Main form (internal partial class MainForm : Form), I need to declare a field : 在我的Main形式(内部局部类MainForm:Form)中,我需要声明一个字段:

AutoRegisterThread<> _currentThread

without specifying the generic type, because I may initiate _currentThread as: 无需指定泛型类型,因为我可以将_currentThread初始化为:

_currentThread=new AutoRegisterThread<implementedClass1> 

or 要么

_currentThread=new AutoRegisterThread<implementedClass2>

_currentThread: will be used across the Form (in many events) _currentThread:将在整个Form中使用(在许多事件中)

When you have a generic class in C#, you have to provide the type parameter. 当您在C#中具有泛型类时, 必须提供type参数。 You could write another class that would not be generic. 您可以编写另一个不会通用的类。 If there is any logic, that should be shared between generic and non-generic classes, you can move that logic to one more new class. 如果存在泛型和非泛型类之间应共享的任何逻辑,则可以将该逻辑移至另一个新类。

Inherit from a non-generic base class: 从非通用基类继承:

internal abstract class AutoRegisterThreadBase { }

internal class AutoRegisterThread<T> : AutoRegisterThreadBase 
   where T: AutoRegisterAbstract
{
field1....
method1...
}

Your main form field can now be of type AutoRegisterThreadBase 您的主表单字段现在可以是AutoRegisterThreadBase类型

Note, if desired, the non-generic parent class can have the same name as the generic class; 注意,如果需要,非泛型父类可以与泛型类具有相同的名称。 in your case, AutoRegisterThread . 在您的情况下,为AutoRegisterThread

EDIT : Extended example, with usage: 编辑 :扩展示例,用法:

internal abstract class AutoRegisterThreadBase { /* Leave empty, or put methods that don't depend on typeof(T) */ }
internal abstract class AutoRegisterAbstract { /* Can have whatever code you need */ }

internal class AutoRegisterThread<T> : AutoRegisterThreadBase
    where T : AutoRegisterAbstract
{
    private int someField;
    public void SomeMethod() { }        
}

internal class AutoRegisterWidget : AutoRegisterAbstract { /* An implementation of AutoRegisterAbstract; put any relevant code here */ }

// A type that stores an AutoRegisterThread<T> (as an AutoRegisterThreadBase)
class SomeType
{
    public AutoRegisterThreadBase MyAutoRegisterThread { get; set; }
}

// Your code that uses/calls the above types
class Program
{        
    static void Main(string[] args)
    {
        var someType = new SomeType();

        // Any sub-class of AutoRegisterThreadBase, including generic classes, is valid
        someType.MyAutoRegisterThread = new AutoRegisterThread<AutoRegisterWidget>();

        // You can then get a local reference to that type 
        // in the code that's created it - since you know the type here
        var localRefToMyAutoRegisterThread = someType.MyAutoRegisterThread as AutoRegisterThread<AutoRegisterWidget>;
        localRefToMyAutoRegisterThread.SomeMethod();
    }
}

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

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