I am using an external DLL which has an abstract baseclass Packet and subclasses to Packet.
The subclasses are named after the number they represent, one name could be P132_RandomString. I am parsing a file containing these numbers and for each number I want to create a corresponding object. There are hundreds of different subclasses and the DLL have no factory method (at least not for my input).
The problem is I don’t know the “RandomString” part, if I did I could have used Reflection but I assume there is no way to use Reflection since I only know the beginning of the classname?
The only solution I could think of is implementing my own factory method with hundreds of case statements but this feels a bit cumbersome…
My question is: is there a nice way to do this?
You can use a LINQ Where clause to grab the type you want.
Consider the following program:
(This assumes you know the base class and the prefix)