473,411 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 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 2526
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
1
by: Thomas D. | last post by:
Hello all, I'm using the IXmlSerializable interface for a project and encounter some problems when testing my webservice in a client application. I know this interface is undocumented and not...
1
by: louis_la_brocante | last post by:
Dear all, I am having trouble generating a client proxy for a webservice whose methods return a "complex" type. The type is complex in that it is a class whose members are a mix of primitive...
12
by: Aff | last post by:
Brothers , i am facing a problem which is as follow: class poly { private: char name; int capacity; public: poly(char , int );
8
by: WakeBdr | last post by:
I'm writing a class that will query a database for some data and return the result to the caller. I need to be able to return the result of the query in several different ways: list, xml,...
2
by: ELINTPimp | last post by:
Hello all, Have a really interesting problem (at least to me) with my upload_file() function. I have it working now, with a bit of a work around, but would like to know what everyone thinks in...
8
sammyboy78
by: sammyboy78 | last post by:
I'm trying to create a class "WeeklyPay" that contains the methods that class "WeeklyPayTest" will use to compute the weekly pay of an employee when the user inputs employee name, hours worked, and...
2
by: thuythu | last post by:
Please help me.... I used and Javascript to view the data. But when i click button open a popup windows, then select data and click save button. The popup close and return the main page, but the...
1
by: Sudarhan | last post by:
Hello frnds I have created a webbased form using asp and javascript .. while submitting the form i am validating the fields in the form .it validates the field and returns alert message. but when...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.