简体   繁体   English

StackTrace ByRef到VB.NET中的另一个类

[英]StackTrace ByRef to another class in VB.NET

I'm writing a class, that must be added as dll file to any .NET project. 我正在写一个类,必须将其作为dll文件添加到任何.NET项目中。 I'm stuck in point, where I need to make event to System.Diagnostics.StackTrace "last_method_name_changed". 我陷入困境,我需要在System.Diagnostics.StackTrace中创建事件“ last_method_name_changed”。

So I need to find a way to detect changes in StackTrace, or create event that is raised when thread enters or exits a method. 因此,我需要找到一种方法来检测StackTrace中的更改,或者创建在线程进入或退出方法时引发的事件。 Something like: 就像是:

Public Class DllClass
    Public Shared CallerTrace as StackTrace = Nothing
    Public Shared New(ByRef NewTrace as StackTrace) 'This is called from form.
        '''I know this will make a copy, but I have failed to find a solution.
        CallerTrace = NewTrace
    End Sub
    Public Shared Sub StackTrace_Changed() Handles CallerTrace.Changed
        MsgBox(CallerTrace.GetFrame(0).GetMethod.Name)
    End Sub
End Class

From the main form this should be called only once: 在主要形式中,仅应调用一次:

Dim DllCls as New DllClass(New Diagnostics.StackTrace)

I'll be grateful for any help in making "reference link" or pointer to StackTrace and event that will raise when CallerTrace.GetFrame(0).GetMethod.Name is changed. 我将不胜感激在“参考链接”或指向StackTrace的指针以及更改CallerTrace.GetFrame(0).GetMethod.Name时将引发的事件方面的任何帮助。

No copy is made as StackTrace is a reference type (assigning to a reference doesn't copy the underlying object). 由于StackTrace是引用类型,因此不会进行任何复制(分配给引用不会复制基础对象)。 The ByRef in the parameter declaration is unrelated and redundant. 参数声明中的ByRef是无关且冗余的。

On the other hand you didn't declare CallerTrace as WithEvents so you don't actually listen to any events that are raised by it. 另一方面,您没有将CallerTrace声明为WithEvents因此您实际上不监听它引发的任何事件。 That won't work though, since System.Diagnostics.StackTrace declares no public events that can be listened to. 但是,这将不起作用,因为System.Diagnostics.StackTrace声明没有可以监听的公共事件。 Your approach for intercepting method calls fundamentally doesn't work. 您用于拦截方法调用的方法基本上是行不通的。

To intercept method calls in the fashion that you envisage you need to drill way further. 要以您设想的方式拦截方法调用,您需要进一步钻取。 Luckily there are libraries which already allow that, such as the AOP library PostSharp . 幸运的是,已经有一些库允许这样做,例如AOP库PostSharp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM