首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

请问一个通过字符串指定类型的方法

2011-12-23 
请教一个通过字符串指定类型的方法请高手指教类型指定问题。例如:dimstypeasstringstype myclass myclas

请教一个通过字符串指定类型的方法
请高手指教类型指定问题。
例如:
dim   stype   as   string  
stype= "myclass "   'myclass   是我自己定义的一个class
下面我想声明一个myclass类的对象,也就是通过字符串来指定变量类型。
请问该如何编码实现?


[解决办法]
Type myType1 = Type.GetType( "myclass "); //这里命名要注意
object o = Activator.CreateInstance (myType1)

[解决办法]
查看帮助文档中关于 "反射 "的主题~
[解决办法]
Type.GetType(这里是全名,甚至用到强类名,包括版本号等)
自定义的,全名就可以了.

比如:
Dim t As DataBase
t = CType(System.Activator.CreateInstance( _
Type.GetType( "SimpleWorksApplication.DataBase ")), DataBase)

热点排行