For Programmers: Free Programming Magazines  


Home > Archive > C# > June 2004 > Class Name of inheritor









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Class Name of inheritor
Robert

2004-06-03, 7:31 pm

I have an abstract base class in which I want to be able to know the
name of the class that is inheriting it. I have a way of gaining this
information, but it seems faulty in that it will only work if the
calling function of the property is in the subclass. It wouldn't work
if I referenced the property in the base class.

Does anyone know of a better way of getting a subclasses name in the
base class?

Code I currently have:

abstract class MyBaseClass
{
static protected string MyName
{
get
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(1);
MethodBase mb = sf.GetMethod();
return mb.DeclaringType.FullName;
}
}
}


class MySubClass : MyBaseClass
{
[STAThread]
static void Main(string[] args)
{
Console.Out.WriteLine(MyName);
}
}


Many thanks in advance!
Rob
Eric Means

2004-06-03, 7:31 pm

rtennys@hotmail.com (Robert) wrote in message news:<6e21d614.0405250656.679cfaf6@posting.google.com>...
> Does anyone know of a better way of getting a subclasses name in the
> base class?


I haven't tried it, but would this.GetType().FullName do what you want?
Robert

2004-06-08, 8:57 pm

I appreciate the reply, but since the property is static, there is no "this" to use.

Thanks!
Rob


> I haven't tried it, but would this.GetType().FullName do what you want?

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com