C# Generics - Accepting different types -


i trying write generic method can avoid code duplication. generic method has able accept 1 of 3 different grid view types cannot following cast work @ start of generic method;

var grid;              if (typeof(t) == typeof(gridview))             {                 grid = (gridview)gridview;             }             else if (typeof(t) != typeof(bandedgridview))             {                 grid = (bandedgridview)gridview;             }             else if (typeof(t) != typeof(advbandedgridview))             {                 grid = (advbandedgridview)gridview;             }             else return; 

how can cast "grid" either of 3 types can them. still trying grasp idea , concept behind generics.

if brandedgridview , advbrandedgridview both inherit gridview can add constraint generic

...<t> t : gridview

if not can use convert.changetype:

try convert.changetype:

if (typeof(t) == typeof(gridview)) {     var grid = (gridview)convert.changetype(gridview, typeof(gridview)); } elseif (typeof(t) == typeof(brandedgridview)) {     var grid = (brandedgridview)convert.changetype(gridview, typeof(brandedgridview)); } 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -