Determine namespace of assembly

  • Thread starter Thread starter Chris Becker
  • Start date Start date
C

Chris Becker

The following code works. My problem is that I don't want to have to
hardcode the string that I set resourceName to in the code. Is there a way
for me to determine the namespace of the assembly from the oAssembly object?
All I want is the string "MyTopNamespace.Helpers." I will of course hard
code the USStates.xml.

namespace MyTopNamespace.Helpers.SpecialCollections
{
public sealed class USCollections
{

private static DataTable FetchStateData()
{
// open the executing assembly
System.Reflection.Assembly oAssembly =
System.Reflection.Assembly.GetExecutingAssembly();

// create stream for resource in assembly
// Make sure that the resource's build action in the project is set to
'Embedded Resource'
string resourceName = "MyTopNamespace.Helpers.USStates.xml";
System.IO.Stream docStream =
oAssembly.GetManifestResourceStream(resourceName);

DataSet ds = new DataSet();
ds.ReadXml(docStream, XmlReadMode.Auto);
ds.Tables[0].TableName = "USStates";

return ds.Tables[0].Copy();
}
}
}
 
I found out the way to do it if I make the function non-static:

this.GetType().Namespace

will give me what I need. Is there a way to do it when the function is
static?
 
have you tried something like:

typeof(YourType).Namespace ?


Chris Becker said:
I found out the way to do it if I make the function non-static:

this.GetType().Namespace

will give me what I need. Is there a way to do it when the function is
static?


Chris Becker said:
The following code works. My problem is that I don't want to have to
hardcode the string that I set resourceName to in the code. Is there a way
for me to determine the namespace of the assembly from the oAssembly object?
All I want is the string "MyTopNamespace.Helpers." I will of course hard
code the USStates.xml.

namespace MyTopNamespace.Helpers.SpecialCollections
{
public sealed class USCollections
{

private static DataTable FetchStateData()
{
// open the executing assembly
System.Reflection.Assembly oAssembly =
System.Reflection.Assembly.GetExecutingAssembly();

// create stream for resource in assembly
// Make sure that the resource's build action in the project is
set
to
'Embedded Resource'
string resourceName = "MyTopNamespace.Helpers.USStates.xml";
System.IO.Stream docStream =
oAssembly.GetManifestResourceStream(resourceName);

DataSet ds = new DataSet();
ds.ReadXml(docStream, XmlReadMode.Auto);
ds.Tables[0].TableName = "USStates";

return ds.Tables[0].Copy();
}
}
}
 
That works as well... thanks. Since I did change the function into a member
of the object rather than a static function, I will stick with
this.GetType().Namespace so that I don't have to hard code the class's name,
but I will definitely use typeof(typename).Namespace other times.

Thanks for your help.

Chris
Jeffrey Huntsman said:
have you tried something like:

typeof(YourType).Namespace ?


Chris Becker said:
I found out the way to do it if I make the function non-static:

this.GetType().Namespace

will give me what I need. Is there a way to do it when the function is
static?


Chris Becker said:
The following code works. My problem is that I don't want to have to
hardcode the string that I set resourceName to in the code. Is there
a
way
for me to determine the namespace of the assembly from the oAssembly object?
All I want is the string "MyTopNamespace.Helpers." I will of course hard
code the USStates.xml.

namespace MyTopNamespace.Helpers.SpecialCollections
{
public sealed class USCollections
{

private static DataTable FetchStateData()
{
// open the executing assembly
System.Reflection.Assembly oAssembly =
System.Reflection.Assembly.GetExecutingAssembly();

// create stream for resource in assembly
// Make sure that the resource's build action in the project is
set
to
'Embedded Resource'
string resourceName = "MyTopNamespace.Helpers.USStates.xml";
System.IO.Stream docStream =
oAssembly.GetManifestResourceStream(resourceName);

DataSet ds = new DataSet();
ds.ReadXml(docStream, XmlReadMode.Auto);
ds.Tables[0].TableName = "USStates";

return ds.Tables[0].Copy();
}
}
}
 
Back
Top