How to get Namespace of an Assembly?

点点圈 提交于 2019-11-27 21:04:32

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();
Lance H

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!