1

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?

4 Answers 4

2

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)

class Program {
    static void Main( string[] args ) {

        string prefix = "p22";
        IEnumerable<Type> types = Assembly.LoadFrom("c:\\Sample.Assembly.dll").GetTypes();    
        Type baseClass = typeof(foo);
        Type foundType = types.Where(
            t => t.Name.StartsWith( prefix ) &&
                t.IsSubclassOf( baseClass )
                ).SingleOrDefault();
        foo myClass = (foo)Activator.CreateInstance( foundType );
                //Do Stuff with myClass 
    }
}
abstract class foo { }
class p22_notMyClass { }
class p22_myclass : foo { }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. I have to use Assembly.LoadFrom to load the assembly file, but apart from that foundType gets null unless I remove t.IsSubClassOf( baseClass ) which is weird... Once I remove that I get the correct type but then I can't cast to the abstract class foo "InvalidCastException".
Update: It now works using Assembly.LoadFrom, the reason it didn't work first was that I referenced a copy of the DLL... And I got different contexts. sigh
0

To load all types from assembly use Assembly.GetTypes

 var objects = new List<Object>();
 Assembly SampleAssembly;
 SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");
 // Obtain a reference to types known to exist in assembly.
 Type[] types = SampleAssembly.GetTypes();
 foreach(Type t in types)
    if(t.Name.StartsWith("P132")
       objects.Add(Activator.CreateInstance(t));

Use Activator.CreateInstance method described here

Comments

0

you can use Assembly.GetTypes() method and iterate the types and find out which class has the number in its name. Then you can use Activator.CreateInstance to get an instance.

Comments

0

Use Assembly.GetTypes + some String.IndexOf (or other matching) on Type.FullName + Activator.CreateInstance().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.