我可以使用typeof()来创建新对象吗? [英] Can I use typeof() to make new objects?

查看:64
本文介绍了我可以使用typeof()来创建新对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我正在尝试以编程方式决定新对象应该具有哪种类型,

但是typeof-function显然不是答案,如下面的代码

将无法编译。


class AbstractClass:Object {}


class DerivedClass1:AbstractClass {}


类DerivedClass2:AbstractClass {}


静态类程序

{

static void Main()

{

AbstractClass a,b,c;

a = new DerivedClass1();

b = new DerivedClass2();

if(some_condition)

c = new(typeof(a))();

else

c = new(typeof(b))();

}

}


如何实现这个?


最好的问候

Einar V?rnes

解决方案




Einar V?rnes schrieb:


嗨。

我想亲从逻辑上决定新对象应该具有哪种类型,

但是typeof-function显然不是答案,因为以下代码

将无法编译。


类AbstractClass:Object {}

class DerivedClass1:AbstractClass {}


class DerivedClass2:AbstractClass {}


静态课程

{

static void Main()

{

AbstractClass a,b,c;

a = new DerivedClass1();

b = new DerivedClass2();

if(some_condition)

c = new(typeof(a))();

else

c = new(typeof(b))();

}

}


如何实现这个?



查看System.Reflection命名空间。使用typeof,你将获得一个Type对象
。 Assembly类型有一个CreateInstance方法,

将创建一个类型的新对象:


AbstractClass myObject;

/ / ...

类型t = typeof(myObject);

程序集a = Assembly.GetAssembly(t);

AbstractClass newObject = a .CreateInstance(t.FullName);

// ...


Assembly :: CreateInstance有几个签名。所以你可以将
传递给构造函数的参数,例如。

顺便说一下,如果没有提出你的问题,我觉得上课时很困惑

被命名为AbstractClass的但并未宣布为抽象。但那只是味道的美元。 :)


HTH,

Tobi


嗯,你已经创建了实例?在一个简单的层面上,

以下就足够了:


AbstractClass c;

if(some_condition){

c = new DerivedClass1();

} else {

c = new DerivedClass2();

}


还有其他方法:

* Activator.CreateInstance({各种重载})

*来自你可以获得的反射并调用构造函数

*您可以将泛型与:new()一起使用(也许是:

AbstractClass)条款

*您可以在现有实例上使用工厂界面


你能解释一下这个场景,找出一个可能的候选人吗?


注意typeof()适用于类型定义,而不是实例;在您的

代码中,您将使用a.GetType()和b.GetType()来获取Type,

,可用于创建*附加*实例。


Tobias Schr?er schrieb:


AbstractClass myObject;

//。 ..

类型t = typeof(myObject);

程序集a = Assembly.GetAssembly(t);

AbstractClass newObject = a.CreateInstance (t.FullName);

// ...



Argh,刚看到一个错误:


类型t = myObject.GetType(); //不:输入t = typeof(myObject);


这是对的,对不起。


Tobi


Hi.
I am trying to programatically decide which type a new object should have,
but the typeof-function is apparently not the answer, as the following code
will not compile.

class AbstractClass:Object { }

class DerivedClass1 : AbstractClass { }

class DerivedClass2 : AbstractClass { }

static class Program
{
static void Main()
{
AbstractClass a,b,c;
a = new DerivedClass1();
b = new DerivedClass2();
if (some_condition)
c = new (typeof(a))();
else
c = new (typeof(b))();
}
}

How can implement this?

Best regards
Einar V?rnes

解决方案

Hi,

Einar V?rnes schrieb:

Hi.
I am trying to programatically decide which type a new object should have,
but the typeof-function is apparently not the answer, as the following code
will not compile.

class AbstractClass:Object { }

class DerivedClass1 : AbstractClass { }

class DerivedClass2 : AbstractClass { }

static class Program
{
static void Main()
{
AbstractClass a,b,c;
a = new DerivedClass1();
b = new DerivedClass2();
if (some_condition)
c = new (typeof(a))();
else
c = new (typeof(b))();
}
}

How can implement this?

Take a look into the System.Reflection namespace. With typeof you will
get a Type object. The Assembly type has a CreateInstance method, which
will create a new object of a type:

AbstractClass myObject;
// ...
Type t = typeof(myObject);
Assembly a = Assembly.GetAssembly(t);
AbstractClass newObject = a.CreateInstance(t.FullName);
// ...

There are several signatures for Assembly::CreateInstance. So you can
pass arguments to the constructor, for example.
Btw and not concering your question, I find it confusing to have a class
that is named "AbstractClass" but is not declared abstract. But that
just a metter of "taste" :)

HTH,
Tobi


Well, you already created the instances? At a simple level, the
following should suffice:

AbstractClass c;
if(some_condition) {
c = new DerivedClass1();
} else {
c = new DerivedClass2();
}

There are other approaches:
* Activator.CreateInstance({various overloads})
* from reflection you can obtain and invoke the constructor
* you can use generics with the ": new() " (and perhaps " :
AbstractClass") clause(s)
* you can perhaps use a factory interface on existing instances

Can you explain a little more about the scenario, to identify a likely
candidate?

Note that typeof() works on type-definitions, not instances; in your
code you would use a.GetType() and b.GetType() to obtain the Type,
which can be used to create *additional* instances.


Tobias Schr?er schrieb:

AbstractClass myObject;
// ...
Type t = typeof(myObject);
Assembly a = Assembly.GetAssembly(t);
AbstractClass newObject = a.CreateInstance(t.FullName);
// ...

Argh, just saw an error:

Type t = myObject.GetType(); // not: Type t = typeof(myObject);

That would be correct, sorry.

Tobi


这篇关于我可以使用typeof()来创建新对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆