0

I need some help, hope it is doable.

I would like to pass a Class as a variable in C#.

This is my code so you can see what I am trying to do:

object shClass;
switch ((cbxType.SelectedItem as ComboBoxItem).Value)
{
    case "articles":
            shClass = ArticlesClass;
            break;
    case "topics":
            shClass = TopicsClass;
            break;
}
serializer.Deserialize<shClass>(response);

This code does not work, and this just to give you an idea what I need. Is this possible?

I hope you can help me out. Thank you.

caesar
  • 88
  • 7
  • 1
    You can't pass a type through a template argument via a variable. Templates are generated at compile time. Depending on the serializer you are using, you might be able to use the `value.GetType()` to deserialize the value. – Philippe Paré Nov 20 '15 at 01:13
  • That is what I thought. Thank you. – caesar Nov 20 '15 at 01:45
  • It can be done http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method – Antonín Lejsek Nov 20 '15 at 02:22

1 Answers1

0

Why you need to put it outside the switch?? You can try putting it inside instead

switch ((cbxType.SelectedItem as ComboBoxItem).Value)
{
    case "articles":
            serializer.Deserialize<ArticlesClass>(response);
            break;
    case "topics":
            serializer.Deserialize<TopicsClass>(response);
            break;
}
User2012384
  • 4,769
  • 16
  • 70
  • 106