Determine namespace of assembly

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();
}
}
}
 
C

Chris Becker

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?
 
J

Jeffrey Huntsman

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();
}
}
}
 
C

Chris Becker

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();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top