Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to call a function which returns boolean using a variable supplied by the user but to no avail ... Here's the code

Try
    Dim ass As Assembly = Assembly.LoadFile(Application.StartupPath & "\ReflectionLib.dll")
    Dim t_sam As Type = ass.GetType("ReflectionLib")
    Dim hh_m As MethodInfo = t_sam.GetMethod("ShowX", BindingFlags.Public Or BindingFlags.Static)
    hh_m.Invoke(Nothing, New Object() {})
Catch ex As Exception
    MessageBox.Show(ex.Message + ex.StackTrace + vbLf)
End Try


I keep getting Object reference not set to an instance of object error with this code

hh_m.Invoke(Nothing, New Object() {})
Posted
Updated 23-Jan-13 22:58pm
v3
Comments
Abhishek Pant 24-Jan-13 3:46am    
code?
__BrokenArrow__ 24-Jan-13 4:17am    
I want to call a function which returns boolean using a variable supplied by the user but to no avail ... Here's the code


Try
Dim ass As Assembly = Assembly.LoadFile(Application.StartupPath & "\ReflectionLib.dll")
Dim t_sam As Type = ass.GetType("ReflectionLib")
Dim hh_m As MethodInfo = t_sam.GetMethod("ShowX", BindingFlags.Public Or BindingFlags.Static)
hh_m.Invoke(Nothing, New Object() {})
Catch ex As Exception
MessageBox.Show(ex.Message + ex.StackTrace + vbLf)
End Try

I keep getting Object reference not set to an instance of object error with this code

hh_m.Invoke(Nothing, New Object() {})
PIEBALDconsult 24-Jan-13 3:48am    
Why? What function? What have you tried so far? Wouldn't you rather go bowling?
__BrokenArrow__ 24-Jan-13 4:17am    
I want to call a function which returns boolean using a variable supplied by the user but to no avail ... Here's the code


Try
Dim ass As Assembly = Assembly.LoadFile(Application.StartupPath & "\ReflectionLib.dll")
Dim t_sam As Type = ass.GetType("ReflectionLib")
Dim hh_m As MethodInfo = t_sam.GetMethod("ShowX", BindingFlags.Public Or BindingFlags.Static)
hh_m.Invoke(Nothing, New Object() {})
Catch ex As Exception
MessageBox.Show(ex.Message + ex.StackTrace + vbLf)
End Try

I keep getting Object reference not set to an instance of object error with this code

hh_m.Invoke(Nothing, New Object() {})
PIEBALDconsult 24-Jan-13 4:22am    
You still didn't answer why.

Put breakpoint at line 1 in code you posted and step through using F10. See which object is null and figure out why.
 
Share this answer
 
The following code solved my hick up concerning the Invocation of the method... I kept on getting the error that my type was in fact null, so I browsed on old posts and found this solution, though it has to be tweaked a little to suit my needs it works fine. thanks for your valued input I look forward to working with you soon


Imports System.Reflection

Public Class Form1

Private asm As System.Reflection.Assembly
Private t As Type()
Private ty As Type
Private m As MethodInfo()
Private mm As MethodInfo

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Here wec have to load the assembly of classlibrary by givig the full path of our classLibrary.dll

asm = System.Reflection.Assembly.LoadFrom(Environment.CurrentDirectory + "\dll\ReflectionLib.dll")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t = asm.GetTypes()
For i As Integer = 0 To t.Length - 1
ListBox1.Items.Add(t(i).FullName)
Next i
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
mm = ty.GetMethod(ListBox2.SelectedItem.ToString())
Dim o As Object
o = Activator.CreateInstance(ty)
Dim oo As Object() = {Int32.Parse(TextBox1.Text), Int32.Parse(TextBox2.Text)}
'If there is no parameter in method then on the place of oo pass the null

TextBox3.Text = mm.Invoke(o, oo).ToString()
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
ListBox2.Items.Clear()
ty = asm.GetType(ListBox1.SelectedItem.ToString())
m = ty.GetMethods()
For j As Integer = 0 To m.Length - 1
ListBox2.Items.Add(m(j).Name)
Next j
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900