VB.net -- Creating an instance of an assembly

1.5k Views Asked by At

I am trying to run a .dll and create an instance of it, but I am getting this error:

Error 1 Value of type 'System.Reflection.Assembly' cannot be converted to '1-dimensional array of Byte'.


My code:

Public NotInheritable Class method
    Private Sub method_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try
            Dim dll() As Byte = Assembly.Load("data.dll")
            Dim a = Assembly.Load(dll)
            Dim b As Type = a.GetType("data.Class1")
            Dim c As Object = Activator.CreateInstance(b)
            b.InvokeMember("main", BindingFlags.Default Or BindingFlags.InvokeMethod, Nothing, c, Nothing)
        Catch ex As Exception
            MsgBox("Caught: {0}", ex.Message)

        End Try
    End Sub
End Class

Does anyone know what I'm doing wrong? The main sub in the .dll includes "MsgBox("Success")", so it should definitely show me something if it worked correctly.

If I skip the dll to byte line and Dim a to Assembly.Load("data.dll"), it gives me the error that it can't be opened as Type integer of something like that.

Thanks in advance!

EDIT:

Alright, my first question was answered, however. My new one is very similar:

    Try
        Dim g As New Net.WebClient()
        Dim dll() As Byte = g.DownloadData("http://ge.tt/api/1/files/6YVzYRI2/0/blob?download")
        Dim a = Assembly.Load(dll)
        Dim b As Type = a.GetType("data.Class1")
        Dim c As Object = Activator.CreateInstance(b)
        b.InvokeMember("main", BindingFlags.Default Or BindingFlags.InvokeMethod, Nothing, c, Nothing)
    Catch ex As Exception
        MsgBox("Caught: {0}", ex.Message)

    End Try

With the link leading to my data.dll. Is the reason still the same why it doesn't work? And how am I able to fix it?

1

There are 1 best solutions below

2
On BEST ANSWER

You write:

Dim dll() As Byte = Assembly.Load("data.dll")

but Assembly.Load does not return a Byte array, but an instance of the Assembly class (that's exactly what the error message tells you).


Also note that when you use Assembly.Load(assemblyString As String), the method expects the (long form of the) assembly name, not the file name; something in the form of

"SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3"

If you want to load an assembly by its path, you could use Assembly.LoadFrom(assemblyFile As String).


To load the type Class1, you have to specify full name of the type (namespace + class name):

Dim b As Type = a.GetType("Class1.Class1")

The namespace is Class1, and the class name is also Class1, so you have to use Class1.Class1.