运行时对象创建 [英] Runtime Object creation

查看:74
本文介绍了运行时对象创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启动应用程序时,必须读取配置文件。 config

文件提供了需要创建的对象的详细信息。根据配置文件中的信息

,需要在运行时实例化对象,并且必须调用其

方法。我该怎么做?


反射是唯一的方法吗?还有其他办法吗?

When the Application is started, it has to read a config file. The config
file gives details of the objects that need to be created. Based on the info
from the config file, the objects need to be instantiated at run time and its
methods have to be invoked. How do I do this?

Is reflection the only way to do it? Is there any other way?

推荐答案

" Tim" < Ti*@discussions.microsoft.com> écritdansle message de news:
FF ********************************** @ microsoft.com ...


|启动应用程序时,它必须读取配置文件。配置

| file提供了需要创建的对象的详细信息。基于

信息

|从配置文件中,对象需要在运行时实例化,并且



|必须调用方法。我该怎么做?

|

|反思是唯一的方法吗?还有其他方法吗?


看看Class Factory的设计模式。


Joanna


-

Joanna Carter [TeamB]

顾问软件工程师
"Tim" <Ti*@discussions.microsoft.com> a écrit dans le message de news:
FF**********************************@microsoft.com...

| When the Application is started, it has to read a config file. The config
| file gives details of the objects that need to be created. Based on the
info
| from the config file, the objects need to be instantiated at run time and
its
| methods have to be invoked. How do I do this?
|
| Is reflection the only way to do it? Is there any other way?

Look at the Class Factory design pattern.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


使用Activator.CreateInstance()


" Tim"写道:
use Activator.CreateInstance()

"Tim" wrote:
当应用程序启动时,它必须读取配置文件。 config
文件提供了需要创建的对象的详细信息。根据配置文件中的信息,需要在运行时实例化对象,并且必须调用其方法。我该怎么做?

反射是唯一的方法吗?还有其他方法吗?
When the Application is started, it has to read a config file. The config
file gives details of the objects that need to be created. Based on the info
from the config file, the objects need to be instantiated at run time and its
methods have to be invoked. How do I do this?

Is reflection the only way to do it? Is there any other way?






他们提供了一个通用界面吗?


如果是这样你可以用几种方法创建它们,具体取决于你是否知道

程序集,如果它已经加载和其他几个标准,你可以使用

这些方法:


AppDomain.CreateInstance

Assembly.CreateInstance

Activator.CreateInstance

欢呼,


-

Ignacio Machin,

ignacio.machin at dot.state.fl.us

佛罗里达州交通局

Tim < Ti*@discussions.microsoft.com>在留言中写道

新闻:FF ********************************** @ microsof t.com ...
Hi

Do they provide a common interface?

If so you can create them using several ways depending if you know the
assembly, if it''s loaded and several others criterias, you can use one of
these methods:

AppDomain.CreateInstance
Assembly.CreateInstance
Activator.CreateInstance
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:FF**********************************@microsof t.com...
启动应用程序时,必须读取配置文件。 config
文件提供了需要创建的对象的详细信息。基于配置文件中的
info
,需要在运行时实例化对象,并且必须调用其
方法。我该怎么做?

反射是唯一的方法吗?还有其他方法吗?
When the Application is started, it has to read a config file. The config
file gives details of the objects that need to be created. Based on the
info
from the config file, the objects need to be instantiated at run time and
its
methods have to be invoked. How do I do this?

Is reflection the only way to do it? Is there any other way?



这篇关于运行时对象创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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