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
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.
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();
Updated:
IF the assembly name & assembly namespace are same in your project and you sure keep theme same AND you want get the namespace of the current executed Assembly then you can use this:
var namespace = Assembly.GetExecutingAssembly().GetName().Name;
And for your loaded assembly:
var namespace = myAssembly.GetName().Name;
But IF the assembly name & assembly namespace are not same in your project then you can use this way:
// Like @eglasius answer >>
// Find all namespaces in the target assembly where a class with the following name is exists:
var namespaceList=Assembly.Load("MyClassName").GetTypes().Select(t => t.Namespace).Distinct();
Using Mono/Xamarin, you don't have access to the "NameSpace" property. You may use the following instead:
var str = typeof(ATypeInTheAssembly).AssemblyQualifiedName;
return str.Split(',')[1].Trim();
Assembly.GetName().Name will get you the default namespace
To take only the namespace follows the code below:
var assembly = System.Reflection.Assembly.GetAssembly(this.GetType());//Get the assembly object
var nameSpace = assembly.GetType().Namespace;//Get the namespace
OR
public string GetNamespace(object obj)
{
var nameSpace = obj.GetType().Namespace;//Get the namespace
return nameSpace;
}
Getting that an assembly must contains almost a class (or any other type, such as an interface) and they must be contained in a namespace, which has to start within the assembly namespace, the shortest of them will be the last one.
So, here is the solution I found:
public string GetAssemblyNamespace(Assembly asm)
{
string ns = @"";
foreach (Type tp in asm.Modules.First().GetTypes()) //Iterate all types within the specified assembly.
if (ns.Length == 0 ? true : tp.Namespace.Length < ns.Length) //Check whether that's the shortest so far.
ns = tp.Namespace; //If it's, set it to the variable.
return ns; //Now it is the namespace of the assembly.
}
I just find all types within the required assembly and I search for which is contained in the namespace having the shortest name.
来源:https://stackoverflow.com/questions/653128/how-to-get-namespace-of-an-assembly