Home > Net >  Vb.net how to know what a class is class reference?
Vb.net how to know what a class is class reference?

Time:09-22

There is A class A, for example, I'm in class B, C to instantiate the class A, that I have A method in class A is the class BC these two classes to instantiate it?

CodePudding user response:

If debugging is need, can be in A constructor sets A breakpoint, then look at its Call Stack (the Call Stack),
You can also try to use the Environment. The StackTrace to get the call stack,

This reply for debugging purposes, not encourage you as code logic,

CodePudding user response:

Can use a Shared variables and the constructor used to record all of the instances, but the drawback is to pay attention to the processing of garbage collection

CodePudding user response:

refer to the second floor of guiyang horse Ma Shanfu plugging waterproof engineering professional maintenance of swimming pool response:
can use a Shared variables and the constructor used to record all of the instances, but the disadvantage is that attention should be paid to recycling processing

Thank you for your reply, this I can think of, I don't want to pass values, just want a new can get, like form into control name1name2 this name automatically

CodePudding user response:

reference weixin_42150685 reply: 3/f
Quote: refer to the second floor of guiyang horse Ma Shanfu plugging waterproof engineering professional maintenance of swimming pool response:
can use a Shared variables and the constructor used to record all of the instances, but the disadvantage is that attention should be paid to recycling processing

Thank you for your reply, this I can think of, I don't want to pass values, just want a new can get, like form into control automatic name have name1name2 this


Don't write by value, in the no-arg constructor

CodePudding user response:

Entrusted or events can be used, such as class A constructor New (byval ClsName) triggering events, and ClsName is to instantiate A class name

CodePudding user response:

This is very simple, it is ok to get the caller's stack,
Access number and a text message:
 Imports System. Reflection 

Public Class Form1
Private Sub Form1_Load (sender As Object, As EventArgs e) Handles MyBase. Load
Dim m As the New caller
Dim m1 As New caller
MsgBox (callee. CallInfo. Count) 'number is instantiated
End Sub
The End of the Class

Public Class the callee
Public Shared callInfo As New List (Of String)
Sub New ()
Dim trace As New StackTrace
Dim callerlst As String=String. The Empty
For I As Integer=0 To trace.
the FrameCount - 1Callerlst +=trace. GetFrame (I). The GetMethod. ReflectedType. FullName & amp; VbCrLf
Next
If callerlst & lt;> String. The Empty Then
CallInfo. Add (callerlst)
End the If
End Sub
The End of the Class

The Class caller
Dim As a New callee
The End of the Class


Don't text message, or directly to save the caller:
 Imports System. Reflection 

Public Class Form1
Private Sub Form1_Load (sender As Object, As EventArgs e) Handles MyBase. Load
Dim m As the New caller
Dim m1 As New caller
MsgBox (callee. CallInfo. Count) 'number is instantiated
End Sub
The End of the Class

Public Class the callee
Public Shared callInfo As New List (Of MethodBase)
Sub New ()
CallInfo. Add ((New StackTrace). GetFrame (1) GetMethod)
End Sub
The End of the Class

The Class caller
Dim As a New callee
End the Class

CodePudding user response:

GetFrame (1)
Is direct caller, (2) is the caller's caller,,,,,,, and so on,,,

CodePudding user response:

reference clear dawn month 7th floor response:
GetFrame (1)
Is direct caller, (2) is the caller's caller,,,,,,, and so on,,,

Thank you bosses, it appears that in addition to Shared variables also have no other way to solve this problem, the answer and let me learn new knowledge, give bosses, hard work,

CodePudding user response:

How to give points posts for the first time

CodePudding user response:

Lz,!!!!!!

CodePudding user response:

Need to add the tracking code on class A, the cost is too big, is unlikely to be useful, for example: class A referenced several times can be traced, references to dozens of times is very difficult,

CodePudding user response:

If debugging is need, can be in A constructor sets A breakpoint, then look at its Call Stack (the Call Stack),
You can also try to use the Environment. The StackTrace to get the call stack,

This reply for debugging purposes, not encourage you to use as code logic,

CodePudding user response:

If debugging is need, can be in A constructor sets A breakpoint, then look at its Call Stack (the Call Stack),
You can also try to use the Environment. The StackTrace to get the call stack,

This reply for debugging purposes, not encourage you to use as code logic,
  • Related