DerivedType x = new DerivedType();// I don't know what
我需要按对象创建对象实例x。不知怎的,这样的:如何在c中按对象本身获取对象类型#
BaseType y = Activator.CreateInstance (Type.GetType (x));
DerivedType x = new DerivedType();// I don't know what
我需要按对象创建对象实例x。不知怎的,这样的:如何在c中按对象本身获取对象类型#
BaseType y = Activator.CreateInstance (Type.GetType (x));
你可以得到基本类型:
Type baseType = x.GetType().GetTypeInfo().BaseType;
但似乎你做错事一般。
Kirill Bestemyanov,谢谢你的回复。可能是我做错了什么。我需要动态地创建派生类的实例,但我不知道它是什么类型。我需要这样的东西:** BaseClass itemCopy =(itemOfDerivedClass.GetType())Activator.CreateInstance(itemOfDerivedClass.GetType()); **,但编译器不允许做这种类型的对话。 – thedriveee
你想实现的目标是什么? –
我想枚举派生类的属性,处理它们并使用实体框架将该对象写入数据库。 – thedriveee
你为什么这样做?为什么你不能使用泛型或工厂模式? – Dai
'Activator.CreateInstance(x.GetType);'Activator.CreateInstance如何返回对象。或'Activator.CreateInstance(x.GetType.BaseType);' –