0

I have two classes, Class A and Class B. These both classes are lying in two different namespaces. I call a static method of Class B from Class A, then how can I get complete information of Class A and its namespace in called static method of Class B?

I do not want to add any code in class A But can add anything into class B.

NASSER
  • 5,900
  • 7
  • 38
  • 57

4 Answers4

5

Through Reflection.

http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.aspx

marko
  • 10,684
  • 17
  • 71
  • 92
4
obj.GetType().FullName;

or

typeof(AClass).FullName;
L.B
  • 114,136
  • 19
  • 178
  • 224
3

You should use the GetType() method on your A object.

namespace nmspA {
    public class A{
        private void DoSomething(){
            B.Foo(this);
        }
    }
}

namespace nmspB {
    public class B {
        public static void Foo(A a){
            Debug.Write(a.GetType()); // Will write : "nmspA.A"
        }
    }
}
Otiel
  • 18,404
  • 16
  • 78
  • 126
2

How about you pass class A as a parameter to class B? Like:

public class A{
   public void CallB(){
      ClassB.MyMethod(this);
   }
}

public static class B {
   public static void MyMethod(A a){
       // get info about class a here.
   }
}

Of course, you can also look at the options you have with reflection if you do not want to pass the object as a parameter.

Bas Slagter
  • 9,831
  • 7
  • 47
  • 78