返回与名称关联的对象

发布于 2025-01-02 19:03:18 字数 481 浏览 1 评论 0原文

我正在将我的程序连接到一些外部代码。我正在设置它,以便外部代码可以实例化对象,但我遇到了一个问题。我在这里创建了这个函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2025-01-09 19:03:18

仔细阅读Type.GetType()的文档< /a>,具体来说,这部分:

如果typeName包含命名空间但不包含程序集名称,则此方法仅按顺序搜索调用对象的程序集和 Mscorlib.dll。如果 typeName 使用部分或完整的程序集名称进行完全限定,则此方法将在指定的程序集中进行搜索。如果程序集具有强名称,则需要完整的程序集名称。

System.Diagnostics.Process位于System.dll(不是Mscorlib.dll)中,您需要使用完全限定名称。假设您使用的是 .Net 4.0,那就是:

System.Diagnostics.Process, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

如果您不想使用完全限定名称,则可以遍历所有加载的程序集并尝试使用 Assembly.GetType().

Read carefully through the documentation of Type.GetType(), specifically, this part:

If typeName includes the namespace but not the assembly name, this method searches only the calling object's assembly and Mscorlib.dll, in that order. If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly. If the assembly has a strong name, a complete assembly name is required.

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:

System.Diagnostics.Process, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

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().

晚风撩人 2025-01-09 19:03:18

你可以使用类似的东西来创建你的对象。

我定义了一个本地类,并使用了您的流程示例。

Public Class Entry
    Public Shared Sub Main()
        Dim theName As String
        Dim t As Type = GetType(AppleTree)
        theName = t.FullName
        Setup.InstanceOf(theName)

        t = GetType(Process)

        theName = t.FullName & ", " & GetType(Process).Assembly.FullName


        Setup.InstanceOf(theName)

    End Sub
End Class


Public Class Setup
    Shared function InstanceOf(typename As String) as object 
        Debug.Print(typename)
        Dim theType As Type = Type.GetType(typename)
        If theType IsNot Nothing Then
            Dim o As Object = Activator.CreateInstance(theType)
            '
            Debug.Print(o.GetType.ToString)
            return o
        End If
        return nothing 
    End function
End Class

Public Class AppleTree
    Public Sub New()
        Debug.Print("Apple Tree Created")
    End Sub
End Class

you could use something like this for creating your Objects.

I defined a local class and also used your process example.

Public Class Entry
    Public Shared Sub Main()
        Dim theName As String
        Dim t As Type = GetType(AppleTree)
        theName = t.FullName
        Setup.InstanceOf(theName)

        t = GetType(Process)

        theName = t.FullName & ", " & GetType(Process).Assembly.FullName


        Setup.InstanceOf(theName)

    End Sub
End Class


Public Class Setup
    Shared function InstanceOf(typename As String) as object 
        Debug.Print(typename)
        Dim theType As Type = Type.GetType(typename)
        If theType IsNot Nothing Then
            Dim o As Object = Activator.CreateInstance(theType)
            '
            Debug.Print(o.GetType.ToString)
            return o
        End If
        return nothing 
    End function
End Class

Public Class AppleTree
    Public Sub New()
        Debug.Print("Apple Tree Created")
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文