I was searching for a common question but couldn't find any solution even after googling. Maybe I am searching wrong?
Is it possible to know from which class the object has been created? For Example: In Visual Basic Code:
I have a class,
Public Class dummyA
End Class
I have another class,
Public Class dummyMain
Dim dmmA As New dummyA
End Class
Can I check in dummyA
, if the object has been created from dummyMain?
Answer with c# or VB.Net would be great. Thanks.
Easiest way is to have an overloaded constructor and pass the owner into it.
public class DummyA
{
public DummyA(object owner)
{
var createdByDummyMain = owner is DummyMain;
}
}
and then do
public class DummyMain
{
public DummyMain()
{
var dmmA = new DummyA(this);
}
}
There is also this but it won't give you exactly what you want. There are other proposals too that deal with StackFrame
but it isn't reliable due to JIT optimizations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.