为了账号安全,请及时绑定邮箱和手机立即绑定

获取派生类型类

获取派生类型类

C#
拉丁的传说 2021-04-26 17:17:09
因此,我具有以下结构:public abstract class MyBase{    public Type TargetType { get; protected set; }}public class A : MyBase{    public A()    {        TargetType = GetType();//Wrong, I need B class type not C    }}public class B : A{    public B() { }}public class C : B{    public C() { }}当然,我可以通过这种方式接收我的类型:public class B : A{    public B()    {        TargetType = typeof(B);    }}实际上,我必须编写一些代码以使示例更清晰:Class1.cspublic static Dictionary<Type, Type> MyTypes = new Dictionary<Type, Type>(){    { typeof(B),typeof(BView) }}public Class1(){    C itemC = new C();    Class2.Initialize(itemC);}Class2.cspublic static Initialize(MyBase myBase){    Type t;    Class1.MyTypes.TryGetValue(myBase.TargetType, out t);    //I need get BView but I get null because *myBase.TargetType* is C class type}等级结构:级别0:(MyBase)-1个对象级别1:(A)-2个对象2级:(B)-100个以上对象3级:(C)-80个对象及更多我把这个案子放在括号里我将不胜感激
查看完整描述

2 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

在任何对象实例上,您可以调用.GetType()以获取该对象的类型。

您不需要在建筑上设置类型



查看完整回答
反对 回复 2021-05-08
  • 2 回答
  • 0 关注
  • 126 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信