How to define namespace name and class name programmatically?

A

Alex

How I can programmatically define the name of namespace
and the name of class?

using System;

namespace MyNamespace
{

class MyClass
{

[STAThread]
static void Main(string[] args)
{

MyClass mc = new MyClass();
mc.GetNamespaceandClassNames();

}

public void GetNamespaceandClassNames()
{
string NamespaceName="";
string ClassName="";

NamespaceName = ??? //Should
be "MyNamespace"

ClassName = ??? //Should
be "MyClass"


}
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Alex,

You will have to get the Type of your class. You can easily do this by
calling the GetType method on yourself, like so:

// Get the type.
Type pobjType = this.GetType();

Once you have that, you can use the Namespace property on the Type
object to get the namespace, and you can use the Name property to get the
name of the type.

One suggestion. You might want to make this code static, as it can be
applied to ANY type, ANYWHERE. There is no use loading the instance with
another method when the logic itself has this kind of affinity.

Hope this helps.
 

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

Similar Threads


Top