Festivejelly
Festivejelly

Reputation: 690

How to get the name of class where an object is initialized c#

I have a few classes. Lets say:

public class A
{
  public void SomeAction()
  {
    Debug.Write("I was declared in class: and my name is:");
  }
}

And

public class B
{
  public static A myClass = new A();
}

public class C
{
  public static A myClass = new A();
}

public class D
{
  public static A myClass = new A();
}

What I want "SomeAction" in class A to do is to print out which class it was initialized in.

So that for example in another class I called C.myClass.SomeAction(); it would print out "I was declared in class C my name is myClass"

I hope this makes sense.

The reasons im doing this is for debugging within automated testing. I understand its not the best way to do things but its a requirement of the business.

Upvotes: 3

Views: 1195

Answers (3)

Jay
Jay

Reputation: 57959

This requirement can be satisfied without inheritance or passing the object; we can get the name of the class that calls the constructor from within the body of the constructor by examining the stack.

public class A
{
    private string _createdBy;

    public void SomeAction()
    {
      Console.WriteLine("I was declared in class [{0}]", _createdBy);
    }

    public A()
    {
      var stackFrame = new StackFrame(1);
      var method = stackFrame.GetMethod();
      _createdBy = method.DeclaringType.Name;
    }
}

In terms of performance, I am assuming that you are not creating many instances of these objects. You could also predicate this on whether you are doing a DEBUG build or on some other setting, so that this stuff is skipped entirely in your production executables.

Upvotes: 7

pgenfer
pgenfer

Reputation: 622

Since you only reference an instance of class A in your other classes, I think there is no other way then setting a reference to the type which created class A, like eddie_cat already mentioned. You could do something like this:

public class B
{
  public static A myClass = new A(typeof(B));
}

And then your class A would look like:

public class A
{
  // store the parent type
  private Type mParentClass; 

  // provide parent type during construction of A
  public A(Type parentClass)
  {
     mParentClass = parentClass;
  }   

  // note that method cannot be static anymore, since every instance of A might 
  // have a different parent
  public void SomeAction()
  {
     // access field where parent type is stored.
     Debug.Write("I was declared in class: {0} and my name is:",mParentClass.Name);
  }
}

Upvotes: 2

Bill Gregg
Bill Gregg

Reputation: 7147

I think you have two choices. Either set a property in A, or inherit from A. Personally, I prefer inheriting from A, because then A could just use GetType().

public class A
{
     public void SomeMethod()
     {
          Debug.Write(string.Format("I was declared in class: {0}",this.GetType()));
     }
}

public class B : A
{


}

var instanceOfB = new B();
instanceOfB.SomeMethod();

Upvotes: 1

Related Questions