使用反射来调用函数 [英] Using reflection to call functions
本文介绍了使用反射来调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想调用一个函数,该函数使用用户提供的变量返回布尔值但无效...这里是代码
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() {})
推荐答案
在您发布的代码中将断点放在第1行并逐步使用F10。查看哪个对象为null并找出原因。
Put breakpoint at line 1 in code you posted and step through using F10. See which object is null and figure out why.
以下代码解决了我对该方法的调用问题的解决方法...我继续得到我的类型实际上是null的错误,所以我浏览了旧帖子并发现了这个解决方案,虽然它必须稍微调整一下以满足我的需求它工作正常。感谢您宝贵的意见我很期待与您合作
Imports System.Reflection
公共类Form1
私有asm As System.Reflection.Assembly
私有t作为类型()
私有类型
私有m作为MethodInfo()
私有mm作为MethodInfo
Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load
''这里我们必须加载classlibrary的程序集givig我们的完整路径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)处理Button1.Click
t = asm.GetTypes()
F.或者我作为整数= 0到t.Length - 1
ListBox1.Items.Add(t(i).FullName)
下一页我
结束Sub
Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理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)}
''如果方法中没有参数那么在oo的位置传递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.ToStr ing())
m = ty.GetMethods()
对于j As Integer = 0到m.Length - 1
ListBox2.Items。添加(m(j).Name)
下一页j
结束子
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
这篇关于使用反射来调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文