使用.GetType()或类似方法创建任意类型的对象...... [英] Using .GetType() or similar to create object of arbitrary type...
问题描述
......或者,或许更具沟通性,这样的事情:
输入someObjectsType = someObject.GetType();
someObjectsType newObject = new someObjectsType();
这可能吗?怎么样,怎么样?
非常感谢提前,
丹尼尔:)
< da ******** @ gmail.com>写道:
...或者,或许,它可能更具沟通性,如下所示:
输入someObjectsType = someObject.GetType();
someObjectsType newObject = new someObjectsType();
您可以使用Activator.CreateInstance从其
类型创建对象。
Eq。
Paul E Collins写道:< da ********@gmail.com>写道:
...或者,或许,它可能更具沟通性,如下所示:
输入someObjectsType = someObject.GetType();
someObjectsType newObject = new someObjectsType();
您可以使用Activator.CreateInstance从其
类型创建对象。
Eq。
嗨保罗:)
感谢您的回复。实际上,我认为在我的
问题中我有点过于通用了。真正发生的是......考虑这两个
(简化)类:
公共抽象类BaseClass
{
//一个尚未定义的项目集合,可以是类型
//" AppointmentCollection"," TaskCollection",或
//ContactCollection,取决于派生类
//被实例化(PimItemCollection是基本类型
//所有这些):
protected PimItemCollection itemCollection;
public BaseClass(PimItemCollection itemCollection)
{
this.itemCollection = itemCollection; < br $>
}
}
公共类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 :)
你可以用反射做到这一点。
你知道反思吗?
问候,
Lars-Inge T?nnessen
....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 :)
<da********@gmail.com> wrote:
...or, to put it perhaps more communicative, something like this:
Type someObjectsType = someObject.GetType();
someObjectsType newObject = new someObjectsType();
You can use Activator.CreateInstance to create an object from its
type.
Eq.
Paul E Collins wrote:<da********@gmail.com> wrote:...or, to put it perhaps more communicative, something like this:
Type someObjectsType = someObject.GetType();
someObjectsType newObject = new someObjectsType();
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 my
question. 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 provide
a "backup" (xml serialization) functionality of the items in a
TaskCollection, ContactCollection, or AppointmentCollection (which
contain 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 works
fine, and I can create xml output from this without problem. But when I
need to restore, it''s another deal. Perhaps because of widening casts
vs. narrowing casts?
Anyway, what I was hoping would work was the following. It''s code
that''s supposed to be in the base class:
// Detect the type of the items in the backup provider''s
itemCollection:
System.Type itemType = itemCollection[0].GetType();
// Read backed up data into a new array:
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();
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 an
assembly reference?)"
So, basically, I don''t really need to create a new object of the
unknown type, but I need to be able to use it like I''m stabbing at
above... Any ideas of how I could do something like that?
Thanks again,
Daniel :)
You can do this with Reflection.
Do you know Reflection?
Regards,
Lars-Inge T?nnessen
这篇关于使用.GetType()或类似方法创建任意类型的对象......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!