41

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

starblue
  • 55,348
  • 14
  • 97
  • 151
SyncMaster
  • 9,754
  • 34
  • 94
  • 137

7 Answers7

38

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.

shA.t
  • 16,580
  • 5
  • 54
  • 111
sharptooth
  • 167,383
  • 100
  • 513
  • 979
25

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
  • 35,831
  • 5
  • 65
  • 110
23

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();
Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98
  • 1
    The assembly name is not the same thing as the assembly's namespace, or the namespace of any particular type within that assembly. So unfortunately, this answer is incorrect. – ErikE Nov 20 '15 at 01:03
  • 3
    @ErikE, Thank u for your hint. I know the question has been answered, my answer was a secondary way for this case: By default they are equal and in some cases developers keep them same, so In this cases my answer is correct. I updated my answer with an additional IF. – Ramin Bateni Nov 21 '15 at 06:28
1

Assembly.GetName().Name will get you the default namespace

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
Lance H
  • 886
  • 1
  • 9
  • 14
1

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;
}
Tiago S
  • 1,299
  • 23
  • 23
  • 2
    This answer is plainly wrong. `assembly.GetType()` returns a `System.Reflection.RuntimeAssembly` object and the namespace for that class is obviously always `System.Reflection`. – Bitterblue Jan 12 '17 at 11:21
  • @Bitterblue The first code helped me at some point, but I do not remember what the application was. As soon as I find where I used the code I put more details – Tiago S Jan 12 '17 at 13:03
1

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();
Jean
  • 4,911
  • 3
  • 29
  • 50
1

Given an assembly should contain at least one type and every type is in a namespace that is in the assembly namespace, the shortest one of them will be the assembly namespace.

public string GetAssemblyNamespace(Assembly asm)
{
    string ns = "";

    foreach (Type tp in asm.Modules.First().GetTypes())
    {
        if (ns.Length == 0 || tp.Namespace.Length < ns.Length)
            ns = tp.Namespace;
    }

    return ns;
}
Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31