获取通过byref参数传递的对象的名称vb.net
问题描述:
如何获取通过byref传入方法的对象的名称?获取通过byref参数传递的对象的名称vb.net
例子:
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,我应该能够得到这个名字,对吧?
MiniTech移动对于谁提供了答案:
这将使你的参数名称的方法,它的类型,而不是传递按地址变量的名称。
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”。
答
这是不可能的。变量的名称不会存储在IL中,只能是类成员或命名空间类的名称。通过引用传递它绝对不会造成差异。你甚至不能打印出“o”。
此外,为什么你会想要这样做?
答
或者,您可以使用反射来获取对象的“类型”。
例子:(使用LinqPad执行)
Sub Main
Dim myDate As DateTime = DateTime.Now
MyMethod(myDate)
Dim something As New Something
MyMethod(something)
End Sub
Public Class Something
Public Sub New
Me.MyProperty = "Hello"
End Sub
Public Property MyProperty As String
End Class
Sub MyMethod(Byref o As Object)
o.GetType().Name.Dump()
End Sub
答
遗憾地说,但是这是你的解决方案。我在方法签名中留下了(ByVal o As Object)
,以防您对此有更多的了解。
Sub MyMethod(ByVal o As Object, ByVal name As String)
Debug.Print(name)
End Sub
Sub Main()
MyMethod(MyObject, "MyObject")
End Sub
另外,您可以创建一个接口,但是这将只允许您使用MyMethod
与您设计的类。您可以做更多的工作来改进它,但是如此代码,您只能在创建时设置RealName
。
Interface INamedObject
Public ReadOnly Property RealName As String
End Interface
Class MyClass
Implements INamedObject
Public Sub New(ByVal RealName As String)
_RealName = RealName
End Sub
Private ReadOnly Property RealName As String Implements INamedObject.RealName
Get
Return _RealName
End Get
End Property
Private _RealName As String
End Class
Module Main
Sub MyMethod(ByVal o As INamedObject)
Debug.Print(o.RealName)
End Sub
Sub Main()
Dim MyObject As New MyClass("MyObject")
MyMethod(MyObject)
End Sub
End Module
答
如果你的程序仍然是相对,使得它的代码相同的地方,这可能工作:
' First get the Stack Trace, depth is how far up the calling tree you want to go
Dim stackTrace As String = Environment.StackTrace
Dim depth As Integer = 4
' Next parse out the location of the code
Dim delim As Char() = {vbCr, vbLf}
Dim traceLine As String() = stackTrace.Split(delim, StringSplitOptions.RemoveEmptyEntries)
Dim filePath As String = Regex.Replace(traceLine(depth), "^[^)]+\) in ", "")
filePath = Regex.Replace(filePath, ":line [0-9]+$", "")
Dim lineNumber As String = Regex.Replace(traceLine(depth), "^.*:line ", "")
' Now read the file
Dim program As String = __.GetStringFromFile(filePath, "")
' Next parse out the line from the class file
Dim codeLine As String() = program.Split(delim)
Dim originLine As String = codeLine(lineNumber * 2 - 2)
' Now get the name of the method doing the calling, it will be one level shallower
Dim methodLine As String = Regex.Replace(traceLine(depth - 1), "^ at ", "")
Dim methodName = Regex.Replace(methodLine, "\(.*\).*$", "")
methodName = Regex.Replace(methodName, "^.*\.", "")
' And parse out the variables from the method
Dim variables As String = Regex.Replace(originLine, "^.*" & methodName & "\(", "")
variables = Regex.Replace(variables, "\).*$", "")
您可以控制的深度,这种挖掘与深度参数堆栈跟踪。 4适用于我的需求。您可能需要使用1 2或3.
它是一个类成员。 –
@Hans Passant:“myobject”?不,这不对。这是一个变量。 – Ryan
用于记录。这是一个没有GUI的过程,我试图不重复代码。如果我可以获得变量名称,那么对于日志来说会很好。我只需要将它作为字符串文字传递给我的方法即可。但是“不”也是一个答案。谢谢。 看到我的问题的代码示例打印“o”(我编辑它)。 – John