❔ Generics
I want to start off by saying I have no clue what I'm talking about. With that out of the way I have a generic function that I want pass a type into, but I want to store that type in a variable and let it be subject to change. Below is an example in a console application which definitively doesn't work, but it demonstrates how I would want it to work.
How is this done? Thanks
9 Replies
you have to use reflection. it is not easy
ok i'll look into it ty
thank you i'll try and get to grips with this. also if i make a list like this
is it automatically casted to A and if so that means I lose infomation on what type it is right? and if so how would I store it instead? sorry for so many questions in one
you do not lose the type information permanently. for example,
aList[0] is B
would be false
, but aList[1] is B
would be true
oh ok
you can also do
aList[1].GetType()
to get typeof(B)
, which you can do other comparisons withok thank you
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.