How to define namespace name and class name programmatically?

  • Thread starter Thread starter Alex
  • Start date Start date
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"


}
}
}
 
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.
 
Back
Top