I have five classes
class Program
{
static void Main(string[] args)
{
Abstract test = new Child();
test.Exectue();
}
}
public abstract class Abstract
{
public void Exectue()
{
IStrategy strategy = new Strategy();
strategy.GetChildClassName();
}
}
public class Child : Abstract
{
}
public interface IStrategy
{
void GetChildClassName();
}
public class Strategy : IStrategy
{
public void GetChildClassName()
{
???
Console.WriteLine();
}
}
My question is, how can I get the name of a Child class (the one that is the instance of test variable) from Strategy class.
Doing this.GetType().Name
yields "Strategy", and
var mth = new StackTrace().GetFrame(1).GetMethod();
var cls = mth.ReflectedType.Name;
yields "Abstract" which is not what i want.
Is there any way that I can get the name of a Child class without doing some weird haxs, like throwing exception or passing type.