vendredi 23 novembre 2018

How to initialize generic class object based on some conditions

I have my generic class Filter, and i need to initialize a object of it.
But based on some user choises, i need to initialize differently, with different types. I tried following:

 Type filterType;

            if (StringRadioBtn.Checked)
            {
                filterType = typeof(string);
            }
            if(DateRadioBtn.Checked)
            {
                filterType = typeof(DateTime);
                IsDate = true;
            }
            if(IntRadioBtn.Checked)
            {
                filterType = typeof(Int64);
                IsInt = true;
            }


    Filters<filterType> f=new Filters<filterType>();//compiler complains about this line


so why does the compiler complaining, and is there way to achieve somethinig like that?

Aucun commentaire:

Enregistrer un commentaire