Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

c# - Windows Forms Generic Inheritance

I have these classes:

class Foo<T1, T2> : Form 
    where T1, T2 : EventArgs

class MiddleGoo : Foo<X,Y>

class Goo : MiddleGoo

X,Y are just simple classes derived from EventArgs.

I see Goo in designer, but i want to create a class Boo between Foo and Goo like this:

class Boo<T1, Y> : Foo<T1, Y>
where T1 : EventArgs

class MiddleGoo : Boo<X,Y>

class Goo : MiddleGoo

Workaround with middle class doesn't work, any ideas?

EDIT: I meant Y and X are classes like YEventArgs and XEventArgs and my problem is about seeing in designer class Boo when i defined Y as T2 but still want to keep it generic through T1.

EDIT2: I just realized I misspelled something about Y class...

public class Foo<T1, T2> : Form
    where T1 : EventArgs
    where T2 : EventArgs
{
}

public class Boo<T1> : Foo<T1, MyEventArgs2>
    where T1 : EventArgs
{
}

public class MiddleGoo : Boo<MyEventArgs1>
{
}

class Goo : MiddleGoo
{
}

public class MyEventArgs2 : EventArgs
{
}

public class MyEventArgs1 : EventArgs
{      
}

And to be clear I just can't see Boo in Designer... ( I can't see MiddleGoo too but i don't need to)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For Visual Studio Version >= VS2015.1

Starting from VS2015.1, Windows Forms Designer shows classes which have generic base classe without any problem. So the workaround which is in other posts is no more required for newer versions of VS and the following class will be shown in designer without any problem.

So having a base generic class like this:

public class BaseForm<TModel,TService> : Form
{
    public TModel Model {get;set;}
    public TService Service {get; set;}
}

You can create the derived form without any problem in designer:

public class FooForm: BaseForm<Foo,FooService> 
{
}

Older Versions of Visual Studio

In older versions of Visual Studio, when the designer wants to host your form in designer, it tries to create an instance of base class of your form, and your class must have a non-generic base to so the designer can show it.

So you can see BaseForm<T>:Form can be shown in designer but CategoryForm:BaseForm<Category> can not be shown in designer. As a workaround in these cases you should create a BaseCategoryForm:BaseForm<Category> and then CategoryForm:BaseCategoryForm will be shown in designer.

Example

Suppose this is your base class that accepts TModel as Model and TService as Service for example:

public class BaseForm<TModel,TService> : Form
{
    public TModel Model {get;set;}
    public TService Service {get; set;}
}

Then create an intermediate form this way, with this line of code:

Public Class BaseFooForm: BaseForm<Foo, FooService>{ }

And the final form this way:

public class FooForm: BaseFooForm
{
}

Now the final FooForm has designer and you can work with it normally. This way you can create your classes to be supported in designer.

Note

The update is also applied on control designer. So also in generic base class for WinForm UserControl you no more need such workaround for VS>=VS2015.1 .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...