c# - How to know the derived class in the base class -


i've searched everywhere on , can't find doing similar. i'm quite newbie @ c# having worked 15 years non-object orientated language, hope i'm not missing simple.

i trying make standard winform get's inherited, don't need repeat code use every time (one of joys of .net on i've used years ability centralize things far more).

my problem comes want implement ability either call derived classes 'single instance' or 'multiple instance' when launched mdi , after much searching i've achieved using 'generics'. i've got stuck how know class current form is, pass generic class close form.

a simplified version of code understand problem. generic class designed launching , closing form. determines if instance t exists, depending on if launching single or multiple instance either shows that, or creates new instance of form. mdi passed in launch methods allow me attach new form that.

public class formlaunch<t> t : mybaseform, new() {     public static t instance;     public static void launchultipleinstnace(frmmdi mdi)     {         instance = new t();         instance.mdiparent = mdi;         instance.show();         instance.bringtofront();     }     public static void launchsingleinstance(frmmdi mdi)     {         if (instance == null) instance = new t();         instance.mdiparent = mdi;         instance.show();         instance.bringtofront();     }     public static void closeinstance()     {         instance = null;     } } 

a 'single instance' version of form launched mdi using following statement.

        formlaunch<myderivedclass>.launchsingleinstance(this); 

the problem comes handle closing of form once in base form , not have every time in derived forms. can't work out how this.

public partial class mybaseform : form {     public mybaseform()     {         initializecomponent();     }     private void mybaseform_formclosed(object sender, formclosedeventargs e)     {         formlaunch<this.gettype()>.closeinstance();     } } 

but this.gettype() not work. if using form directly rather inheriting use

        formlaunch<mybaseform>.closeinstance(); 

or if handled formclosed event in every derived class use

        formlaunch<myderivedclass>.closeinstance(); 

but want in way stops me forgetting every time in derived class... because wrote few months ago , have forgotten pretty every time i've used it.

edit: question really, pass in place of t in

        formlaunch<t>.closeinstance() 

this needs refer derived class , can't seem find that. this.gettype() refers correct glass not work.

you can subscribe formclosed in launchsingleinstance() , remove handling of close event form completely:

public static void launchsingleinstance(frmmdi mdi) {     if (instance == null)     {         instance = new t();         instance.formclosed += (s, e) => closeinstance();     }     instance.mdiparent = mdi;     instance.show();     instance.bringtofront(); } 

a more direct answer - possible using reflection:

typeof(formlaunch<>).makegenerictype(this.gettype())                     .getmethod("closeinstance")                     .invoke(null, null); 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -