-1

I have two classes

class ClassA{
    public ClassA(){
        // when this class is instantiated I want to know "who" is creating an instance of it?
        // In this case the answer should be 'ClassB'
    }
}

class ClassB{
    public SomeFunc(){
        // Do some stuff

        var a = new ClassA();
    }
}

and I want to know when ClassB creates an instance of ClassA that is was ClassB who created a new instance of ClassA. How is this accomplished?

Robert Green MBA
  • 1,834
  • 1
  • 22
  • 45
  • 2
    why do you want to do this? knowing why can help us determine the best solution. – ps2goat Feb 08 '17 at 17:40
  • 1
    Does this question/answer help? Haven't tried it with classes: http://stackoverflow.com/questions/171970/how-can-i-find-the-method-that-called-the-current-method – James Hunt Feb 08 '17 at 17:40
  • 1
    By adding a parameter to the constructor and expecting the caller to pass itself. Not sure why you want this though, loose coupling is one of the principles of OOP that you really should adhere to. – Igor Feb 08 '17 at 17:41
  • 1
    why not pass some `string` var to the `ClassA` constructor? the value should be an identification of who's calling the constructor. – Mobigital Feb 08 '17 at 17:43
  • 3
    This sounds like an XY problem. You're asking us how to do X when you really want to solve Y. – Moo-Juice Feb 08 '17 at 17:47

3 Answers3

2

You can use the System.Diagnostics.StackTrace class:

Type callingType = new StackTrace().GetFrame(1).GetMethod().DeclaringType;
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
1

how about pass an identification of who is calling:

class ClassA{
    public ClassA(string caller){
        // when this class is instantiated I want to know "who" is creating an instance of it?
        // In this case the answer should be 'ClassB'
        Console.WriteLine("Caller="+caller);
    }
}

class ClassB{
    public SomeFunc(){
        // Do some stuff 
        var a = new ClassA(nameof(ClassB));
    }
}

output:

enter image description here

Mobigital
  • 749
  • 7
  • 14
0

As many other already asked, I would like to know as well on the why you want this. Is it needed at runtime and if so why? Otherwise a good logging mechanism could be of help maybe?

You could pass the information. Or, if you want to automate that you could use one of these attributes that gives information about the caller: https://msdn.microsoft.com/en-us/library/mt653988.aspx. A possible example using these attributes:

class ClassA
{
    public ClassA([CallerMemberName]string member = "", [CallerFilePath]string classFile = ""){
        // when this class is instantiated I want to know "who" is creating an instance of it?
        // In this case the answer should be 'ClassB'
        Console.WriteLine("Created using method {0} from file {1}", member, classFile);
    }
}

class ClassB
{
    public void SomeFunc(){
        // Do some stuff

        var a = new ClassA();
    }
}

This outputs:

Created using method SomeFunc from file c:\Users\Peter\Temp\ClassB.cs

Now, if you do create a .cs file per class as many do it is easy to extract the classname from the filename. The good thing about this approach is that you do not have to code the caller names so there is no tight coupling.

Peter Bons
  • 26,826
  • 4
  • 50
  • 74