所以,我有以下结构: 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() { }}p
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.cs
public static Dictionary<Type, Type> MyTypes = new Dictionary<Type, Type>() { { typeof(B),typeof(BView) } } public Class1() { C itemC = new C(); Class2.Initialize(itemC); }
Class2.cs
public 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个物体等等
我把这个案子放在括号里
我将不胜感激任何帮助
在对象的任何实例上,您可以调用.GetType()来获取该对象的类型.您无需在构造上设置类型