我有五个班级
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();
}
}
我的问题是,如何从 Strategy 类中获取子类的名称(即测试变量的实例)。
这样做this.GetType().Name会产生“策略”,并且
var mth = new StackTrace().GetFrame(1).GetMethod();
var cls = mth.ReflectedType.Name;
产生“摘要”,这不是我想要的。
有什么方法可以让我在不做一些奇怪的 hax 的情况下获得 Child 类的名称,比如抛出异常或传递类型。
30秒到达战场
慕娘9325324
相关分类