Shankar Acharya
Shankar Acharya

Reputation: 53

How to know the calling Class Name?

Is there a way know which class has called a function in another class. Eg:

public class A
{
    public static string Aa = "test";
    public void test()
    {
        B.testB();
    }
}

public class B
{
    public static void testB()
    {
        string Bb = A.Aa;

    }
}

In the above example, i know the class A function has called the function in class B. But if there are many classes which will call the function in class B and all of those classes will have variable Aa in common, so how can i read its value and assign it to Bb. So in simple

string Bb = CalledClassName.Aa;

Upvotes: 0

Views: 3933

Answers (5)

poke
poke

Reputation: 388303

You could use the CallerMemberNameAttribute that was added with .NET 4.5. This will only get you the member name though:

public void SomeMethod ()
{
    OtherMethod();
}

public void OtherMethod ([CallerMemberName] string memberName = null)
{
    Console.WriteLine(memberName);
}

The attribute will fill the optional parameter at compile time, so it will actually call OtherMethod("SomeMethod").


You could also use a combination of accessing the stack trace and using reflection to read the Aa property of the type of the calling method. Note that this accesses debugging information, and is very vulnerable to changes in your code. It also has a bad performance, so you should avoid it. But just to show you how to use it:

public static void testB()
{
    StackTrace stackTrace = new StackTrace();
    Type callingType = stackTrace.GetFrame(1).GetMethod().DeclaringType;
    FieldInfo field = callingType.GetField("Aa", BindingFlags.Public | BindingFlags.Static);
    string Bb = (string) field.GetValue(null);

    Console.WriteLine(Bb);
}

Upvotes: 7

Revils
Revils

Reputation: 1508

I geuss you can do something like this:

public class A {

    public void test() {
        B.testB(this);
    }
}

public class B {
    public static void testB(object sender) {
        String className = sender.GetType().Name;

    }
}

//To call
A a = new A();
a.test();

Upvotes: 0

Patrick Hofman
Patrick Hofman

Reputation: 157098

I guess you are looking for something different than you are asking.

You should pass the instance of A to your method. All calling methods should pass the instance based on an interface. In that interface you put the properties and methods you want to share. In that way you can call the 'same' method for every passed instance.

public interface ISomeInterface
{
    string Aa {get;}
}

public class A : ISomeInterface
{
    public string Aa {get { return "a"; } }
}

Then you can pass it to this method:

public static void testB(ISomeInterface something)
{
    string Bb = something.Aa;
}

Note that in this case, Aa is not allowed to be static. You could wrap that static in an instance property though.

Upvotes: 2

Fabjan
Fabjan

Reputation: 13676

If i understood your question correctly then you can pass a reference to a class instance in method as parameter then use 'is' operator to check its type:

public class A
{
    public static string Aa = "test";
    public void test(object calledClass)
    {
        if(calledClass is B) Aa = calledClass.Bb;
    }
}

When you call this static method from class B just put :

A.Test(this)

P.S. This is just an example of logic that you can use to achieve what you want

Upvotes: 0

Amit
Amit

Reputation: 46361

Use an interface, pass that in:

public interface AaInterface {
   public string GetAa();
}

public class A : AaInterface 
{
    public static string Aa = "test";
    public GetAa() { return Aa; }
    public void test()
    {
        B.testB(this);
    }
}

public class B
{
    public static void testB(AaInterface pAa)
    {
        string Bb = pAa.GetAa();
    }
}

Upvotes: 5

Related Questions