Click to See Complete Forum and Search --> : Reflection: Type.GetType(string) but different Assembly


snoof
January 10th, 2008, 11:09 AM
Hi!

I've got a string representing the full name of a class ("Blah.Muh.Thing" for example). For getting it's type I normally would now use Type.GetType(string). But the class isn't in the calling Assembly. It's in a DLL used by my application. So I have to use Assembly.GetType(string) first. BUT that only works when I know the Assembly. Now my question:

What do I have to do if I want to load the type from a string if I don't know from which Assembly it comes from?

[C#/.NET2.0/VS 2005]

jasonli
January 10th, 2008, 11:21 AM
I think you need to know what assembly(namespace) where the type definition is.

wildfrog
January 10th, 2008, 11:38 AM
You could do something like this:

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

List<Type> types = new List<Type>();

foreach (Assembly assembly in assemblies)
{
Type type = assembly.GetType("LibA.MyType");
if (type != null)
types.Add(type);
}

foreach (Type type in types)
{
Console.WriteLine("Fonund type {0} in assembly {1}.", type.FullName, type.Assembly.FullName);
}

- petter

snoof
January 11th, 2008, 05:08 AM
Ah, thank you - works great! I was not able to see the wood for the trees. :D