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.