c# - how to dynamical pass generic-type same as type of parameter? -


i trying this:

public static void showtaskpane(usercontrol type)         {                             var item = set.oftype<typeof(type)>().firstordefault();      } 

but not working, want select object of type or passed parameter. method signature, if required, can change it, suggest me how it?

you can define method as:

public static void showtaskpane<t>() {     var item = set.oftype<t>().firstordefault(); } 

and call like:

showtaskpane<usercontrol>(); 

or if method suppose return control then:

public t showtaskpane<t>() {     return set.oftype<t>().firstordefault(); } 

call like:

var item = showtaskpane<usercontrol>(); 

for comment: there anyway can restrict th type of t subtype of usercontrol?

you can specify constraint like:

public t showtaskpane<t>() t : usercontrol {     return set.oftype<t>().firstordefault(); } 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -