How to return the class name from a class?

  • Thread starter Thread starter ABC
  • Start date Start date
A

ABC

How to return the class name using static getter from a class?

The base class as:

class abcbase
{
..............

public static string ObjectName
{
get { return ??????????? }
}
}

class def : abcbase
{
.....................
}

class ijk : abcbase
{
.......................
}


I want to return "def" if call as:
string strvalue = def.ObjectName;

I want to return "ijk" if call as:
string strvalue = ijk.ObjectName;

How should I do?
 
Hello,

Do you really need a static method ?

If not, you can always use "this.GetType().Name" in your object which will
give you the class name.
 
I really need a static method


Francois Beaussier said:
Hello,

Do you really need a static method ?

If not, you can always use "this.GetType().Name" in your object which will
give you the class name.
 
I don't see a way of doing it with a static method right now.

would the following suit your need ? (even if that is not a static method)

string strvalue = typeof(def).Name;
string strvalue = typeof(ijk).Name;
 
You could say:
string strvalue = typeof(def).Name();

or you could write a generic function:
string typename<T>() { return typeof(T).Name(); }

and then say:
string strvalue = typename<def>();

But it seems silly to go to all that trouble when you could just write:
string strvalue = "def";
 
How to return the class name using static getter from a class?

In a static method you can do

MethodBase.GetCurrentMethod().DeclaringType.Name

I want to return "def" if call as:
string strvalue = def.ObjectName;

I want to return "ijk" if call as:
string strvalue = ijk.ObjectName;

How should I do?

It will not work that way, it will always return "abcbase". Even if
you write def.ObejctName it compiles to abcbase.ObjectName since
that's the class that defines the static member.


Mattias
 
This is an interesting problem on several levels.

First of all, I doubt very much that the effect you ask for you in your
example is possible. Unless I miss my guess, the fact that you can call
the static property abcbase.ObjectName by any of the three names:

abcbase.ObjectName
def.ObjectName
ijk.ObjectName

is just syntactic sugar offered by the compiler. The actual call at
runtime would be to abcbase.ObjectName, clearly on the "called" side,
but also on the calling side, but I may be wrong about that. However,
if I'm right, there would be no trace left at runtime that you said
"ijk.ObjectName" instead of "abcbase.ObjectName".

Second, I can't even find a way, using Reflection, to get current
context information such as "what method am I in right now" or "which
class (type) defines the method I am in right now". Of course, one can
get that information using a stack trace, but that seems an awfully
high price to pay to get one stinkin' little class name.

Even in an instance method, all you have is the "this" pointer, but
then that doesn't even tell you what method you're in, or where in the
class hierarchy that method is defined. All it tells you is the type of
object that was instantiated, which may be different from the type that
defined the method that's asking the question.

Anybody out there know, at the very least, how to find the Type that
defines the currently running static property / method, without doing a
stack trace?
 
Thank you, Mattias. MethodBase.GetCurrentMethod() was what I was
looking for when composing my previous reply to this thread.
 
Just out of curiosity - in what way would you plan to actually use this?

a: Since static methods can't be virtual, you can't use inheritance, so you
can't generalise it here and would have to copy it into every class
b: I can only see two ways of actually invoking such a feature at runtime:

directly:
MyClass.ObjectName

through reflection:
PropertyInfo prop = typeof(MyClass).GetProperty("ObjectName",
BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Static);
prop.GetValue(null, null);

In the first instance, you already know (give-or-take aliasing) that the
object is called MyClass

In the second instance (or variants involving being passed a Type or
PropertyInfo object) you call just use the Type's Name property -
e.g.
direct: typeof(MyClass).Name
passed a Type object: type.Name
passed a PropertyInfo object: prop.DeclaringType.Name
passed an object instance: obj.GetType().Name

I'm really just not sure what value your function is adding?

Marc
 
Back
Top