获取通过 byref 参数传递的对象名称 vb.net [英] Get the name of the object passed in a byref parameter vb.net

查看:48
本文介绍了获取通过 byref 参数传递的对象名称 vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取通过 byref 传递给方法的对象的名称?

How can I get the name of the object that was passed byref into a method?

示例:

Dim myobject as object

sub mymethod(byref o as object)
    debug.print(o.[RealName!!!!])
end sub

sub main()
    mymethod(myobject)
    'outputs "myobject" NOT "o"
end sub

我用它来记录日志.我多次使用一种方法,最好记录我传递给它的变量的名称.因为我是通过 byref 传递的,所以我应该可以得到这个名字,对吧?

I'm using this for logging. I use one method multiple times and it would be nice to log the name of the variable that I passed to it. Since I'm passing it byref, I should be able to get this name, right?

对于提供答案的 minitech:

For minitech who provided the answer:

这将为您提供方法中的参数名称及其类型,但不会提供通过引用传递的变量名称.

This would give you the parameter name in the method and it's type, but not the name of the variable that was passed byref.

using system.reflection

Dim mb As MethodBase = MethodInfo.GetCurrentMethod()
For Each pi As ParameterInfo In mb.GetParameters()
    Debug.Print("Parameter: Type={0}, Name={1}", pi.ParameterType, pi.Name)
Next

如果你把它放在上面的mymethod"中,你会得到o"和Object".

If you put that in "mymethod" above you'd get "o" and "Object".

推荐答案

那是不可能的.IL 中不存储变量的名称,只存储类成员或命名空间类的名称.通过引用传递它绝对是零差异.你甚至无法让它打印出o".

That's impossible. Names of variables are not stored in IL, only names of class members or namespace classes. Passing it by reference makes absolutely zero difference. You wouldn't even be able to get it to print out "o".

此外,你为什么要这样做?

Besides, why would you ever want to do that?

这篇关于获取通过 byref 参数传递的对象名称 vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆