views:

1878

answers:

2

Consider i have an assembly(class library dll) which i have loaded using the following code,

Assembly a = Assembly.LoadFrom(@"C:\Documents and Settings\E454935\My Documents\Visual Studio 2005\Projects\nunit_dll_hutt\for_hutt_proj\bin\Debug\asdf.dll");

and i need to get the type of the Assembly. In order to get the type i need the namespace of the assembly.

Type t = asm.GetType("NAMESPACE.CLASSNAME",false,true);

how can i get the Namespace in the above line.?! as inorder to get the Namespace, i need to get the type..?

Type.Namespace;

i.e i need to get the Namespace of the assembly which can be used to get its Type.

Thanks in advance

+4  A: 

Use Assembly.GetTypes() - this will get you a collection of all types and then you can get the Namespace property for each of them.

Then I guess you can simply check that all the types have same Namespace value and use this value. Otherwise add some other logic to detect what namespace to consider primary.

sharptooth
+6  A: 

An assembly can contain multiple namespaces. I think what you really want to ask is how to get a type from an assembly without specifying the namespace.

I don't know if there is a better way, but you can try looking for the specific type like this (add - using linq;):

myassembly.GetTypes().SingleOrDefault(t => t.Name == "ClassName")

This will effectively throw if there is more than 1 class with that name under different namespaces (because the Single method ensures there is only 1).

For a list of the namespaces for that class you can:

Assembly.Load("ClassName").GetTypes().Select(t => t.Namespace).Distinct();
eglasius
In addition, an assembly can contain multiple types even within the same namespace. The OP needs to understand this concept.
Cerebrus