本文介绍了使用.GetType()或类似方法创建任意类型的对象......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! ......或者,或许更具沟通性,这样的事情: 输入someObjectsType = someObject.GetType(); someObjectsType newObject = new someObjectsType(); 这可能吗?怎么样,怎么样? 非常感谢提前, 丹尼尔:) 解决方案 您可以使用Activator.CreateInstance从其 类型创建对象。 Eq。 您可以使用Activator.CreateInstance从其类型创建对象。 Eq。 嗨保罗:) 感谢您的回复。实际上,我认为在我的 问题中我有点过于通用了。真正发生的是......考虑这两个 (简化)类: 公共抽象类BaseClass { //一个尚未定义的项目集合,可以是类型 //" AppointmentCollection"," TaskCollection",或 //ContactCollection,取决于派生类 //被实例化(PimItemCollection是基本类型 //所有这些): protected PimItemCollection itemCollection; public BaseClass(PimItemCollection itemCollection) { this.itemCollection = itemCollection; } } 公共类TasksClass:BaseClass { //这个派生类应该处理任务, //所以它将一个ref传递给一个TaskCollection到 //基类。 br /> public TasksClass() :base(new OutlookSession()。Tasks.Items) { } } 好​​的,基类应该能做的一件事就是提供 a" backup" (xml序列化) TaskCollection,ContactCollection或AppointmentCollection中的项目的功能( 包含PimItem类型的对象:Task,PimItem:Contact,PimItem: 分别预约),不知道前面的特定类型。 从itemCollection中取出PimItem对象的项目 罚款,我可以从此创建xml输出没有问题。但是当我需要恢复时,这是另一笔交易。也许是因为扩大演员阵容 与缩小演员阵容? 无论如何,我希望能够奏效的是以下内容。它应该是基类中的代码 : //检测备份提供程序中项目的类型这是 itemCollection: System.Type itemType = itemCollection [0] .GetType(); //读后备将数据转换为新数组: XmlSerializer xmlSerializer = new XmlSerializer(typeof(itemType [])); StreamReader streamReader = new StreamReader (backupFile); XmlTextReader xmlTextReader = new XmlTextReader(streamReader); itemType [] pimItems = (itemType [])xmlSerializer.Deserialize(xmlTextReader); xmlTextReader.Close(); streamReader.Close(); 文件。删除(backupFile); 在这里,我收到很多错误,说类型或名称空间名称 'itemType''不能发现(你是否错过了使用指令或 汇编参考?) 所以,基本上,我是我真的不需要创建一个未知类型的新对象,但我需要能够使用它,就好像我在上面敲了一下 。 ..关于我怎么做这样的事情的想法? 再次感谢, Daniel :) ....or, to put it perhaps more communicative, something like this:Type someObjectsType = someObject.GetType();someObjectsType newObject = new someObjectsType();Is this possible? If so, how?Thanks very much in advance,Daniel :) 解决方案You can use Activator.CreateInstance to create an object from itstype.Eq. You can use Activator.CreateInstance to create an object from its type. Eq.Hi Paul :)Thanks for your reply. Actually, I think I was a bit too generic in myquestion. What''s really happening is this... Consider these two(simplified) classes:public abstract class BaseClass{// A yet undefined item collection that can be of type// "AppointmentCollection", "TaskCollection", or// "ContactCollection", depending on what derived class// is instantiated (PimItemCollection is the base type// for all these):protected PimItemCollection itemCollection;public BaseClass(PimItemCollection itemCollection){this.itemCollection = itemCollection;}}public class TasksClass : BaseClass{// This derived class is supposed to handle tasks,// so it passes a ref to a TaskCollection to the// base class.public TasksClass(): base(new OutlookSession().Tasks.Items){}}Okay, so one thing that the base class should be able to do, is providea "backup" (xml serialization) functionality of the items in aTaskCollection, ContactCollection, or AppointmentCollection (whichcontain objects of type PimItem : Task, PimItem : Contact, PimItem :Appointment, respectively), without knowing the specific type up front.Pulling out items as PimItem objects from the itemCollection worksfine, and I can create xml output from this without problem. But when Ineed to restore, it''s another deal. Perhaps because of widening castsvs. narrowing casts?Anyway, what I was hoping would work was the following. It''s codethat''s supposed to be in the base class:// Detect the type of the items in the backup provider''sitemCollection:System.Type itemType = itemCollection[0].GetType();// Read backed up data into a new array:XmlSerializer xmlSerializer = newXmlSerializer(typeof(itemType[]));StreamReader streamReader = new StreamReader(backupFile);XmlTextReader xmlTextReader = newXmlTextReader(streamReader);itemType[] pimItems =(itemType[])xmlSerializer.Deserialize(xmlTextReader);xmlTextReader.Close();streamReader.Close();File.Delete(backupFile);Here, I get a lot of errors saying, "The type or namespace name''itemType'' could not be found (are you missing a using directive or anassembly reference?)"So, basically, I don''t really need to create a new object of theunknown type, but I need to be able to use it like I''m stabbing atabove... Any ideas of how I could do something like that?Thanks again,Daniel :) 这篇关于使用.GetType()或类似方法创建任意类型的对象......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-08 07:02