CallByName & ByRef in VB.NET

迈克揉索芙特 2009-10-26 06:25:18
Public Class Class1
Public Function test(ByRef S1 As String) As String
S1 = "second"
End Function
End Class

In my form I do the following:

Dim myObj As New Class1()
Dim local As String = "first"
Call CallByName(myObj , "test", CallType.Method, local)
MsgBox(local)

The byRef dosent seem to be working when I use it with CallByName. Same code works fine in VB but not in VB.NET.

求解中...
...全文
209 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanlongwuhui 2009-10-27
  • 打赏
  • 举报
回复
学习
迈克揉索芙特 2009-10-27
  • 打赏
  • 举报
回复
头发掉一地,问题解决了。
最终还是用的反射,vb.net的CallByName在ByRef参数下有缺陷,不知道算不算一个bug。

至于WCF返回的错误信息,是我的疏忽所致。
错误信息应该从ex.InnerException.Message中获得,而我之前却从ex.Message中获得。
粗心导致走了弯路,不该不该...
迈克揉索芙特 2009-10-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
CallByName函数,调用对象的指定名称的成员方法
反射就是CallByName的.NET版本
在VB.net中通过反射调用
Dim c as New Class1()
Dim type As Type = c.[GetType]()
Dim methodInfo As MethodInfo = type.GetMethod("test", BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.[Public])
Dim args As Object() = New Object() {""}
Dim result As Object = methodInfo.Invoke(target, args)

[/Quote]
谢谢,果然是大佬。
不过我现在用的就是反射,但有问题如下:
我的程序是基于WCF技术的C/S架构,客户端动态调用服务,用如上Object.Invoke(反射)方法时,服务端的错误信息不能到达客户端。反而CallByName却可以正常返回,但缺点是ByRef时的参数返回值让人头痛。
tiandiyouwo1 2009-10-26
  • 打赏
  • 举报
回复
贝隆 2009-10-26
  • 打赏
  • 举报
回复
啊!!你都有难题啊,帮顶。。。
古今多少事 2009-10-26
  • 打赏
  • 举报
回复
hehe,100%Copy!No problem.
wuyq11 2009-10-26
  • 打赏
  • 举报
回复
CallByName函数,调用对象的指定名称的成员方法
反射就是CallByName的.NET版本
在VB.net中通过反射调用
Dim c as New Class1()
Dim type As Type = c.[GetType]()
Dim methodInfo As MethodInfo = type.GetMethod("test", BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.[Public])
Dim args As Object() = New Object() {""}
Dim result As Object = methodInfo.Invoke(target, args)
Iovswety 2009-10-26
  • 打赏
  • 举报
回复

Don't use CallByName method

Public Class Class1 
Public Function test(ByRef S1 As String) As String
S1 = "second"
End Function
End Class

Dim myObj As New Class1()
Dim local As String = "first"
myObj.test(local)
MsgBox(local)

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧