471,326 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

How to return the class name from a class?

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?
Jan 11 '06 #1
8 2334
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.

--
Francois Beaussier

"ABC" <ab*@abc.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP11.phx.gbl...
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?

Jan 11 '06 #2
ABC
I really need a static method
"Francois Beaussier" <francois@beaussier.[remove]net> wrote in message
news:OG**************@TK2MSFTNGP09.phx.gbl...
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.

--
Francois Beaussier

"ABC" <ab*@abc.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP11.phx.gbl...
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?


Jan 11 '06 #3
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;

--
Francois Beaussier
I really need a static method
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.

--
Francois Beaussier

"ABC" <ab*@abc.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP11.phx.gbl...
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?



Jan 11 '06 #4
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";

Jan 11 '06 #5
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?

Jan 11 '06 #6
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 11 '06 #7
Thank you, Mattias. MethodBase.GetCurrentMethod() was what I was
looking for when composing my previous reply to this thread.

Jan 11 '06 #8
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
"ABC" <ab*@abc.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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?

Jan 11 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Michael Maes | last post: by
12 posts views Thread by Aff | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.