So yea, I'm working on a project right now and I need a way to add a reference programmatically on my project. Can somebody tell me how?
You can lazy load using System.Reflection.Assembly.Load()
So yea something like this?
Code:
Dim ass As Assembly = Assembly.LoadFile("D:\ex.dll")
Dim mods As [Module] = ass.GetModule("Module1")
Dim load As MethodInfo = mods.GetMethod("Load")
load.Invoke(Nothing, Nothing)
EDIT:
New Code:
[highlight=vb.net] Public Sub LoadPlugin(ByVal name As String)
Dim ass As Assembly = Assembly.LoadFrom(name)
Dim t As Type = ass.GetType("Test.Plugin.Load")
Dim m As MethodInfo = t.GetMethod("L")
Dim o As Object = Activator.CreateInstance(t)
m.Invoke(o, Nothing)
End Sub[/highlight]
But at Dim m As MethodInfo....etc...It shows me an error, "Reference is not set to an instance of an object."
Posts 1–6 of 6 · Page 1 of 1
Post a Reply
Tags for this Thread
None
Originally Posted by Horatio Caine
So yea something like this?
Code:
Dim ass As Assembly = Assembly.LoadFile("D:\ex.dll")
Dim mods As [Module] = ass.GetModule("Module1")
Dim load As MethodInfo = mods.GetMethod("Load")
load.Invoke(Nothing, Nothing)
EDIT:
New Code:
[highlight=vb.net] Public Sub LoadPlugin(ByVal name As String)
Dim ass As Assembly = Assembly.LoadFrom(name)
Dim t As Type = ass.GetType("Test.Plugin.Load")
Dim m As MethodInfo = t.GetMethod("L")
Dim o As Object = Activator.CreateInstance(t)
m.Invoke(o, Nothing)
End Sub[/highlight]
But at Dim m As MethodInfo....etc...It shows me an error, "Reference is not set to an instance of an object."
Dim t As Type = ass.GetType("Test.Plugin.Load")
Verify that that name actually exists, chances are there is no type with that name.
[highlight=vb.net]
If (ass.GetTypes().Select(Function(T As Type) T.Name).Contains("Test.Plugin.Load")) Then
MessageBox.Show("Type name exists!")
Else
MessageBox.Show("No type with that name exists")
End If
[/highlight]
Well it says that no type with that name exists.
Here's the remote DLL's code:
[highlight=vb.net]Namespace Test.Plugin
Public Class Load
Public Shared Sub L()
MsgBox("Ace")
End Sub
End Class
End Namespace[/highlight]
[highlight=vb.net] Public Sub LoadPlugin(ByVal name As String)
Dim ass As Assembly = Assembly.LoadFrom(name)
For Each x In ass.GetTypes
If x.Name = "Load" Then
Dim m As MethodInfo = x.GetMethod("L")
Dim o As Object = Activator.CreateInstance(x)
m.Invoke(o, Nothing)
End If
Next
End Sub[/highlight]
I've got it working ^^
Thanks a lot guys
@Jason /req closed