Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the root namespace of an assembly?

Given an instance of System.Reflection.Assembly.

like image 711
Andrew Avatar asked Sep 18 '08 20:09

Andrew


People also ask

What is the root namespace?

The root namespace is the mainspace of the . NET Framework libraries. It may happen that someone creates a type or a namespace that will conflict with ones from the . NET Framework.

Which namespace helps to find information of an assembly in C#?

GetType("NAMESPACE.

What is root namespace in VB net?

VB.NET automatically prefixes the root namespace set in the project properties to the namespace of each class. This is different from C#, where the full namespace must be declared each time.

Are assembly and namespace the same?

The main difference between namespace and assembly is that a namespace is a logical group of related classes that can be used by the languages targeted by Microsoft . NET framework, while Assembly is a building block of .


2 Answers

I have come across this dilemma plenty of times when I want to load a resource from the current assembly by its manifest resource stream.

The fact is that if you embed a file as a resource in your assembly using Visual Studio its manifest resource name will be derived from the default namespace of the assembly as defined in the Visual Studio project.

The best solution I've come up with (to avoid hardcoding the default namespace as a string somewhere) is to simply ensure your resource loading code is ALWAYS happening from inside a class that's also in the default namespace and then the following near-generic approach may be used.

This example is loading an embedded schema.

XmlSchema mySchema; string resourceName = "MyEmbeddedSchema.xsd"; string resourcesFolderName = "Serialisation"; string manifestResourceName = string.Format("{0}.{1}.{2}",     this.GetType().Namespace, resourcesFolderName, resourceName); using (Stream schemaStream = currentAssembly.GetManifestResourceStream(manifestResourceName))     mySchema = XmlSchema.Read(schemaStream, errorHandler); 

See also: How to get Namespace of an Assembly?

Edit: Also noticed a very detailed answer to the question I'm answering at http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/3a469f5d-8f55-4b25-ac25-4778f260bb7e

Another edit in case people with same question come looking: Excellent idea to solve the resource-loading question here: How get the default namespace of project csproj (VS 2008)

like image 176
Lisa Avatar answered Sep 21 '22 18:09

Lisa


Not possible. Nothing specifies a "Root" namespace. The default namespace in the options is a visual studio thing, not a .net thing

like image 42
Darren Kopp Avatar answered Sep 23 '22 18:09

Darren Kopp