If I have a class B and C which inherits from A, is there something simpler than using StackFrame's GetFileName() (then parse out the ClassName.cs string)?
If I use this.GetType().Name, it won't return "A" when the code is executing in the parent class.
Sample Code
namespace StackOverflow.Demos
{
class Program
{
public static void Main(string[] args)
{
B myClass = new C();
string containingClassName = myClass.GetContainingClassName(); Console.WriteLine(containingClassName); //should output StackOverflow.Demos.B
Console.ReadKey();
}
}
public class A {public A() { } }
public class B : A { public B() { } }
public class C : B { public C() { } }
}
Pravesh Singh
29-Mar-2014When you say GetType(), or equivalently this.GetType(), you get the actual type of the class. For example
and later:
var myB = new B();
myB.Test(); // writes B to the console
I guess that's what polymorphism is all about.
If you want always A, no polymorphism, simply say: