当类名作为变量传递时,实例化类的最佳方法是什么? [英] Best way to instantiate a class when the class name is passed as variable?
问题描述
我在项目中有很多类似的类,每种类型的报告都有一个类别
我的应用可以创建。我想基于调度程序模块传递的值来实例化它们。
现在我有了
Sub RunReports(sReport)
Select Case sReport
Case" CRByDistrict"
>
案例CRCareConversionRate
oReport =新CRCareConversionRate
案例(等等.....)
结束选择
oReport.Build''所有类实现Build方法
End Sub
例如,如果调度程序说是时候运行CRByDistrict
报告我要传入CRByDistrict并调用
oReport = New< className> ''一行处理所有报告
在这种情况下< className>是CRByDistrict。我已经看了
Activator.CreateInstance和其他一些东西,但我不能指出
这是最好,最简单的方法。
我每次添加新的报告(课程)时都会厌倦维持已成为巨大的Select Case声明
。
>
谢谢,
Brian
I have many similar classes in a project, one for each type of report
my app can create. I want to instantiate them based on a value passed
in by a scheduler module.
Right now I have
Sub RunReports(sReport)
Select Case sReport
Case "CRByDistrict"
oReport = New CRByDistrict
Case "CRCareConversionRate"
oReport = New CRCareConversionRate
Case (etc.....)
End Select
oReport.Build '' All classes implement a Build method
End Sub
For example, if the scheduler says it''s time to run the "CRByDistrict"
report I want to pass in "CRByDistrict" and invoke
oReport = New <className> '' One line handles all reports
where in this case <className> is "CRByDistrict". I''ve looked at
Activator.CreateInstance and some other things, but I can''t figure out
the best, simplest way to do this.
I''m tired of maintaining what has become a huge Select Case statement
each time I add a new report (class).
Thanks,
Brian
推荐答案
恕我直言,Activactor.CreateInstance应该是你的选择。既然你已经在同一个项目中拥有了所有的类(因此也是相同的程序集),那么使用这种方法就可以非常简单了。类似于:
dim reportType = Type.GetType(sReport)
dim reportObject = Activator.CreateInstance(reportType)
或
dim reportObject = Activator.CreateInstance(Nothing,sReport)
希望有所帮助..
Imran。
" Brian" <峰; br ****** @ hotmail.com>在消息中写道
news:b6 ************************** @ posting.google.c om ...
IMHO, Activactor.CreateInstance should be the way to go for you. Since you
have all your classes in the same project (and hence the same assembly), it
should be pretty simple to use this method. Something like:
dim reportType = Type.GetType(sReport)
dim reportObject = Activator.CreateInstance(reportType)
Or
dim reportObject = Activator.CreateInstance(Nothing, sReport)
hope that helps..
Imran.
"Brian" <br******@hotmail.com> wrote in message
news:b6**************************@posting.google.c om...
我在项目中有很多类似的类,每个类型的报告都有一个我的应用可以创建的类。我想基于调度程序模块传递的值来实例化它们。
现在我有了Sub RunReports(sReport)
选择案例报告
案例CRByDistrict
oReport =新CRByDistrict
案例CRCareConversionRate
oReport =新CRCareConversionRate
案例(等等.....)
结束选择
oReport.Build''所有类实现Build方法
End Sub
例如,如果调度程序表示现在是时候运行我要传入的CRByDistrict报告CRByDistrict了。并调用
oReport = New< className> ''一行处理所有报告
在这种情况下< className>是CRByDistrict。我已经看过了
Activator.CreateInstance和其他一些东西,但我无法弄明白这是最好,最简单的方法。
我是'我厌倦了维护已成为巨大的Select Case声明
每次我添加一个新的报告(类)。
谢谢,
Brian
I have many similar classes in a project, one for each type of report
my app can create. I want to instantiate them based on a value passed
in by a scheduler module.
Right now I have
Sub RunReports(sReport)
Select Case sReport
Case "CRByDistrict"
oReport = New CRByDistrict
Case "CRCareConversionRate"
oReport = New CRCareConversionRate
Case (etc.....)
End Select
oReport.Build '' All classes implement a Build method
End Sub
For example, if the scheduler says it''s time to run the "CRByDistrict"
report I want to pass in "CRByDistrict" and invoke
oReport = New <className> '' One line handles all reports
where in this case <className> is "CRByDistrict". I''ve looked at
Activator.CreateInstance and some other things, but I can''t figure out
the best, simplest way to do this.
I''m tired of maintaining what has become a huge Select Case statement
each time I add a new report (class).
Thanks,
Brian
在文章< b6 ************************** @ posting.google.com> ,Brian写道:
In article <b6**************************@posting.google.com >, Brian wrote:
我在项目中有很多类似的类,每个类型的报告都有一个我的应用可以创建的类。我想基于调度程序模块传递的值来实例化它们。
现在我有了Sub RunReports(sReport)
选择案例报告
案例CRByDistrict
oReport =新CRByDistrict
案例CRCareConversionRate
oReport =新CRCareConversionRate
案例(等等.....)
结束选择
oReport.Build''所有类实现Build方法
End Sub
例如,如果调度程序表示现在是时候运行我要传入的CRByDistrict报告CRByDistrict了。并调用
oReport = New< className> ''一行处理所有报告
在这种情况下< className>是CRByDistrict。我已经看过了
Activator.CreateInstance和其他一些东西,但我无法弄明白这是最好,最简单的方法。
我是'我厌倦了维护已成为巨大的Select Case声明
每次我添加一个新的报告(类)。
谢谢,
Brian
I have many similar classes in a project, one for each type of report
my app can create. I want to instantiate them based on a value passed
in by a scheduler module.
Right now I have
Sub RunReports(sReport)
Select Case sReport
Case "CRByDistrict"
oReport = New CRByDistrict
Case "CRCareConversionRate"
oReport = New CRCareConversionRate
Case (etc.....)
End Select
oReport.Build '' All classes implement a Build method
End Sub
For example, if the scheduler says it''s time to run the "CRByDistrict"
report I want to pass in "CRByDistrict" and invoke
oReport = New <className> '' One line handles all reports
where in this case <className> is "CRByDistrict". I''ve looked at
Activator.CreateInstance and some other things, but I can''t figure out
the best, simplest way to do this.
I''m tired of maintaining what has become a huge Select Case statement
each time I add a new report (class).
Thanks,
Brian
我假设所有报告类都实现了特定的
接口或从公共基类继承 - 但对于这个例子
我''假设一个名为IReport的接口定义了Build方法。
鉴于上述情况,这样的事情应该有效:
Private Sub RunReport(ByVal) ReportType As String)
Dim report As IReport = DirectCast _
(Activator.CreateInstance(Type.GetType(ReportType)),_
IReport )
report.Build()
结束子
-
Tom Shelton [MVP]
I am assuming that all of the report classes implement a specific
interface or inherit from a common base class - but for this example
I''ll assume an interface named IReport that has the Build method defined.
Given the above, something like this should work:
Private Sub RunReport (ByVal ReportType As String)
Dim report As IReport = DirectCast _
(Activator.CreateInstance (Type.GetType (ReportType)), _
IReport)
report.Build()
End Sub
--
Tom Shelton [MVP]
oops..that wouldn 甚至编译;-)
我的意思是:
dim reportType As Type = Type.GetType(sReport)
dim reportObject As Object = Activator.CreateInstance(reportType)
或
dim reportObject as Object = Activator.CreateInstance(Nothing ,sReport)
" Imran Koradia" <无**** @ microsoft.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
oops..that wouldn''t even compile ;-)
I meant:
dim reportType As Type = Type.GetType(sReport)
dim reportObject As Object = Activator.CreateInstance(reportType)
Or
dim reportObject as Object = Activator.CreateInstance(Nothing, sReport)
"Imran Koradia" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
恕我直言, Activactor.CreateInstance应该是你的方式。既然你把所有的类放在同一个项目中(因此同一个程序集),
使用这个方法应该非常简单。类似于:
dim reportType = Type.GetType(sReport)
dim reportObject = Activator.CreateInstance(reportType)
或
dim reportObject = Activator.CreateInstance(Nothing,sReport)
希望有所帮助..
Imran。
Brian <峰; br ****** @ hotmail.com>在消息中写道
新闻:b6 ************************** @ posting.google.c om ...
IMHO, Activactor.CreateInstance should be the way to go for you. Since you
have all your classes in the same project (and hence the same assembly), it should be pretty simple to use this method. Something like:
dim reportType = Type.GetType(sReport)
dim reportObject = Activator.CreateInstance(reportType)
Or
dim reportObject = Activator.CreateInstance(Nothing, sReport)
hope that helps..
Imran.
"Brian" <br******@hotmail.com> wrote in message
news:b6**************************@posting.google.c om...
我在项目中有很多类似的类,每个类型的报告都有一个我的应用可以创建的类。我想基于调度程序模块传递的值来实例化它们。
现在我有了Sub RunReports(sReport)
选择案例报告
案例CRByDistrict
oReport =新CRByDistrict
案例CRCareConversionRate
oReport =新CRCareConversionRate
案例(等等.....)
结束选择
oReport.Build''所有类实现Build方法
End Sub
例如,如果调度程序表示现在是时候运行我要传入的CRByDistrict报告CRByDistrict了。并调用
oReport = New< className> ''一行处理所有报告
在这种情况下< className>是CRByDistrict。我已经看过了
Activator.CreateInstance和其他一些东西,但我无法弄明白这是最好,最简单的方法。
我是'我厌倦了维护已成为巨大的Select Case声明
每次我添加一个新的报告(类)。
谢谢,
Brian
I have many similar classes in a project, one for each type of report
my app can create. I want to instantiate them based on a value passed
in by a scheduler module.
Right now I have
Sub RunReports(sReport)
Select Case sReport
Case "CRByDistrict"
oReport = New CRByDistrict
Case "CRCareConversionRate"
oReport = New CRCareConversionRate
Case (etc.....)
End Select
oReport.Build '' All classes implement a Build method
End Sub
For example, if the scheduler says it''s time to run the "CRByDistrict"
report I want to pass in "CRByDistrict" and invoke
oReport = New <className> '' One line handles all reports
where in this case <className> is "CRByDistrict". I''ve looked at
Activator.CreateInstance and some other things, but I can''t figure out
the best, simplest way to do this.
I''m tired of maintaining what has become a huge Select Case statement
each time I add a new report (class).
Thanks,
Brian
这篇关于当类名作为变量传递时,实例化类的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!