Get Static Member of Parent Class from Child Inherited Class

Let's say I have,

public class A
{
    public static void MyAMethod()
    {
    }

    public class B
    {

    }        
}

public class C : A.B
{
    public void MyCMethod()
    {
        MyAMethod();// I need to call this
    }
}

Now I need to call MyAMethod from class C.

Edit: In my situation class A is unavailable. So, I cannot use A.MyAMethod.

Upvotes: 1

Views: 873

Answers (6)

VolkerK
VolkerK

Reputation: 96159

If you take a look at the IL code for

namespace NestedTest
{
    public class A
    {
        public static void MyAMethod()
        {
            System.Console.WriteLine("public static void MyAMethod()");
        }

        public class B
        {
            public void MyBMethod()
            {
                MyAMethod();
            }
        }
    }
}

you will find that MyBMethod is implemented(?) as

.method public hidebysig instance void  MyBMethod() cil managed
{
  // Code size       8 (0x8)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  call       void NestedTest.A::MyAMethod()
  IL_0006:  nop
  IL_0007:  ret
} // end of method B::MyBMethod

As you can see the call to NestedTest.A::MyAMethod() is hard-coded i.e. the "magic" was already done by the C#->IL compiler. (*)
You could get the information you need to call the static method via reflection, e.g. (without error handling and rather crude)

public class C : A.B
{
    public void MyCMethod()
    {
        Type parent = GetType().BaseType;
        Type outer = parent.DeclaringType;
        System.Reflection.MethodInfo mi = outer.GetMethod("MyAMethod");
        mi.Invoke(null, null);
        return;
    }
}

but there is probably a better solution for your specific problem.


(*) and the c# compiler does that only as specified in http://msdn.microsoft.com/en-us/library/ms228593.aspx

3.7 Scopes

The scope of a name is the region of program text within which it is possible to refer to the entity declared by the name without qualification of the name [...]

- The scope of a member declared by a class-member-declaration (§10.1.6) is the class-body in which the declaration occurs. In addition, the scope of a class member extends to the class-body of those derived classes that are included in the accessibility domain (§3.5.2) of the member.

class B is part of the class-body of class A, class C is not. And class C is also not derived from class A.

Upvotes: 2

Rahul
Rahul

Reputation: 77866

You can't call MyAMethod() that way directly cause it doesn't belong to class B and you are inheriting class B; instead it's a static member of class A and Hence you have to qualify it before calling A.MyAMethod();

If you would have inherited class A instead then your code would do fine cause class C will inherit the method as well.

OR

Change your code to accomodate what you want like

public class A
    {
        public static void method()
        {
            Console.WriteLine("method in base class");  
        }
    public class B
    {
        public void bmethod()
        {
            A.method(); 
        }
    }
}

public class C : A.B
{
    public void cmethod()
    {
        bmethod(); 
    }
}

Upvotes: 0

A static method, field, property, or event is callable on a class even when no instance of the class has been created. And according to the data given in Staic Members Functions. You can call a static members directly with the Class name. So, in your case

A.MyAMethod(); will do the stuff.

Upvotes: 0

Grrbrr404
Grrbrr404

Reputation: 1805

You set MyAMethod to be static, so just call A.MyAMethod() inside your MyCMehtod()

Regards

Upvotes: 0

Haris Hasan
Haris Hasan

Reputation: 30097

Simply do

public class C : A.B
{
    public void MyCMethod()
    {
        A.MyAMethod();// I need to call this
    }
}

As the method is static you can call it from anywhere through class A

Upvotes: 1

aqwert
aqwert

Reputation: 10789

You can just call A.MyAMethod() from inside your method since it is static

Upvotes: 4

Related Questions