code_junkie

IS there any way to get a reference to the calling object in c#?

Nov 13th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. class stackTest
  2. {
  3. public void Test()
  4. {
  5. StackFrame sFrame = new StackFrame(1);
  6. if (sFrame == null)
  7. {
  8. Console.WriteLine("sFrame is null");
  9. return;
  10. }
  11.  
  12. var method = sFrame.GetMethod();
  13.  
  14. if (method == null)
  15. {
  16. Console.WriteLine("method is null");
  17. return;
  18. }
  19. Type declaringType = method.DeclaringType;
  20. Console.WriteLine(declaringType.Name);
  21. }
  22.  
  23. public void Test2()
  24. {
  25. Console.WriteLine(new StackFrame(1).GetMethod().DeclaringType.Name);
  26. }
  27. }
  28. class Program
  29. {
  30. static void Main(string[] args)
  31. {
  32.  
  33. stackTest s = new stackTest();
  34. s.Test();
  35. Console.WriteLine("Doing Test2");
  36. s.Test2();
  37. Console.ReadLine();
  38.  
  39. }
  40. }
Add Comment
Please, Sign In to add comment