返回与名称关联的对象
我正在将我的程序连接到一些外部代码。我正在设置它,以便外部代码可以实例化对象,但我遇到了一个问题。我在这里创建了这个函数:
Public Function InstanceOf(ByVal typename As String) As Object
Dim theType As Type = Type.GetType(typename)
If theType IsNot Nothing Then
Return Activator.CreateInstance(theType)
End If
Return Nothing
End Function
我正在尝试创建一个 System.Diagnostics.Process 对象。但无论出于何种原因,它总是返回 Nothing
而不是对象。有人知道我做错了什么吗?
我在 VB.net 中执行此操作,因此所有 .net 响应都会被接受:)
I'm connecting my program to some external code. I'm setting it up so that the external code can instance objects and I've come across a problem. I've created this function here:
Public Function InstanceOf(ByVal typename As String) As Object
Dim theType As Type = Type.GetType(typename)
If theType IsNot Nothing Then
Return Activator.CreateInstance(theType)
End If
Return Nothing
End Function
I'm trying to create a System.Diagnostics.Process
object. For what ever reason though, it always return Nothing
instead of the object. Does anybody know what I'm doing wrong?
I'm doing this in VB.net so all .net responses are accepted :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仔细阅读
Type.GetType()
的文档< /a>,具体来说,这部分:自
System.Diagnostics.Process
位于System.dll(不是Mscorlib.dll)中,您需要使用完全限定名称。假设您使用的是 .Net 4.0,那就是:如果您不想使用完全限定名称,则可以遍历所有加载的程序集并尝试使用
Assembly.GetType().
Read carefully through the documentation of
Type.GetType()
, specifically, this part:Since
System.Diagnostics.Process
is in System.dll (not Mscorlib.dll), you need to use the fully qualified name. Assuming you're using .Net 4.0, that would be:If you don't want to work with fully qualified names, you can go through all loaded assemblies and try to get the type using
Assembly.GetType()
.你可以使用类似的东西来创建你的对象。
我定义了一个本地类,并使用了您的流程示例。
you could use something like this for creating your Objects.
I defined a local class and also used your process example.