以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  咨询一个函数写法  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=46803)

--  作者:jianjingmaoyi
--  发布时间:2014/2/27 19:40:00
--  咨询一个函数写法
我在内部函数这样写了一段代码:

Dim DllName As String ="测试dll.dll"
Dim Name As String ="测试dll"
Dim Typename As String = "AfterOpenProject"
Dim methodname As String = "AfterOpenProject"
Dim ParamArray0() As Object = Nothing
Dim assembly0 As system.reflection.assembly = system.reflection.assembly.loadfrom(ProjectPath & DllName)
Dim method As System.Reflection.MethodInfo = assembly0.Gettype(Name & "." & Typename).GetMethod(methodname)
Dim obj2 As object
Try
    obj2 = method.Invoke(Nothing, New Object() { ParamArray0 })
Catch exception1 As Exception
    Dim exception As Exception = exception1
    
    MessageBox.Show(("调用DLL执行出错,错误信息:" & exception.InnerException.ToString), "警告", MessageBoxButtons.OK, MessageBoxIcon.Hand)
    goto Label_Error
End Try
Return obj2

Label_Error:
Return Nothing

 执行函数为:

Functions.Execute("ABC","测试dll.dll","AfterOpenProject","AfterOpenProject",New Object() { "生产管理系统" })  

 执行没有任何问题

但是我这样改后:

Dim Typename As String = Args(0)
Dim methodname As String = Args(1)
Dim ParamArray0() As Object = Nothing
Dim assembly0 As system.reflection.assembly = system.reflection.assembly.loadfrom(ProjectPath & "测试dll.dll")
Dim method As System.Reflection.MethodInfo = assembly0.Gettype("测试dll" & "." & Typename).GetMethod(methodname)
Dim obj2 As object
Try
    obj2 = method.Invoke(Nothing, New Object() { ParamArray0 })
Catch exception1 As Exception
    Dim exception As Exception = exception1
    
    MessageBox.Show(("调用DLL执行出错,错误信息:" & exception.InnerException.ToString), "警告", MessageBoxButtons.OK, MessageBoxIcon.Hand)
    goto Label_Error
End Try
Return obj2

Label_Error:
Return Nothing

 函数应该一样,但是执行后提示找不到实例了

--  作者:逛逛
--  发布时间:2014/2/27 19:58:00
--  
你的参数量和传入数为什么是不一样的啊?
--  作者:逛逛
--  发布时间:2014/2/27 20:05:00
--  

Dim obj As object =  Functions.Execute("ABC","AfterOpenProject","AfterOpenProject")  

 

还得判断空

 

猜的

[此贴子已经被作者于2014-2-27 20:06:17编辑过]

--  作者:jianjingmaoyi
--  发布时间:2014/2/27 20:24:00
--  
我修改了函数是可以执行,但是现在是写在dll的不能执行
--  作者:jianjingmaoyi
--  发布时间:2014/2/27 20:33:00
--  
我把函数改成这样是可以执行了:

Dim ParamArray0() As Object = Nothing
Dim assembly0 As system.reflection.assembly = system.reflection.assembly.loadfrom(ProjectPath & "测试dll.dll")
Dim method As System.Reflection.MethodInfo = assembly0.Gettype("测试dll" & "." & Args(0)).GetMethod(Args(1))
Dim obj2 As object
Try
    obj2 = method.Invoke(Nothing, New Object() { ParamArray0 })
Catch exception1 As Exception
    Dim exception As Exception = exception1
    
    MessageBox.Show(("调用DLL执行出错,错误信息:" & exception.InnerException.ToString), "警告", MessageBoxButtons.OK, MessageBoxIcon.Hand)
    goto Label_Error
End Try
Return obj2

Label_Error:
Return Nothing

执行函数为:

Functions.Execute("ABC","AfterOpenProject","AfterOpenProject",New Object() { "生产管理系统" })  

 但是改成另外一个就又不行了.方法一样


--  作者:有点甜
--  发布时间:2014/2/27 21:32:00
--  
 我感觉是LoadFrom的问题


--  作者:jianjingmaoyi
--  发布时间:2014/2/27 21:53:00
--  
不应该呀 问题是已经成功,只是另外一个不成功了.
--  作者:逛逛
--  发布时间:2014/2/28 11:33:00
--  

试一下这样,参数自己改

 

Dim filePath As String = Args(0)    \'文件路径
Dim className As String = Args(1)    \'模块名
Dim methodName As String = Args(2)    \'函数名
Dim paramter() As Object  = Args(3)   \'参数集

If FileSys.FileExists(filePath ) = False Then   
    MessageBox.Show("文件不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question)
    Return Nothing
End If

Dim fileName As String = system.io.Path.GetFileNameWithoutExtension(filePath)  \'取文件名
Try
        Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile(filePath)
        Dim classtemp As Type = asm.Gettype(fileName & "." & className,True)
        Dim obj As Object = asm.CreateInstance(classtemp.FullName)
        Dim me_Info As System.Reflection.MethodInfo = classtemp.GetMethod(methodName)        
        Return me_Info.Invoke(obj, paramter)
Catch exception1 As Exception
    Dim exception As Exception = exception1
   
    MessageBox.Show(("调用DLL执行出错,错误信息:" & exception.InnerException.ToString), "警告", MessageBoxButtons.OK, MessageBoxIcon.Hand)
    Return Nothing
End Try

 

 

 

调用

 

Dim str As String = "E:\\shishi\\测试.dll"
        Dim paramter(1) As Object
        paramter(0) = 1
        paramter(1) = 5
 Dim ob As object = Functions.Execute("动态DLL",str,"Class1","aa",paramter)
If ob IsNot Nothing Then
Output.show(ob)
End If