简体   繁体   English

从子继承的类获取父类的静态成员

[英]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. 现在,我需要从类C调用MyAMethod。

Edit : In my situation class A is unavailable. 编辑 :在我的情况下,A类不可用。 So, I cannot use A.MyAMethod. 因此,我不能使用A.MyAMethod。

您可以只从方法内部调用A.MyAMethod() ,因为它是静态的

If you take a look at the IL code for 如果您看一下IL代码

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 您会发现MyBMethod被实现为(?)

.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 ie the "magic" was already done by the C#->IL compiler. 如您所见,对NestedTest.A :: MyAMethod()的调用是硬编码的,即,“魔术”已经由C#-> IL编译器完成。 (*) (*)
You could get the information you need to call the static method via reflection, eg (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 (*),而c#编译器仅按照http://msdn.microsoft.com/zh-cn/library/ms228593.aspx中指定的方式执行该操作

3.7 Scopes 3.7范围

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. -由class-member-declaration(第10.1.6节)声明的成员范围是发生声明的类主体。 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. 另外,类成员的范围扩展到成员的可访问性域(第3.5.2节)中所包含的那些派生类的类主体。

class B is part of the class-body of class A, class C is not. class B是A class C的类主体的一部分, class C不是。 And class C is also not derived from class A. class C也不是从A类派生的。

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 由于该方法是静态的,因此您可以通过A类从任何地方调用它

You set MyAMethod to be static, so just call A.MyAMethod() inside your MyCMehtod() 您可以设置MyAMethod是静态的,所以才称之为A.MyAMethod()您MyCMehtod内()

Regards 问候

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 . 并根据Staic成员函数中给出的数据。 You can call a static members directly with the Class name. 您可以使用类名称直接调用静态成员。 So, in your case 所以,在你的情况下

A.MyAMethod(); will do the stuff. 会做的事情。

You can't call MyAMethod() that way directly cause it doesn't belong to class B and you are inheriting class B; 您不能以这种方式直接调用MyAMethod() ,因为它不属于class B并且您是在继承类B。 instead it's a static member of class A and Hence you have to qualify it before calling A.MyAMethod(); 相反,它是class A的静态成员,因此您必须在调用A.MyAMethod();之前A.MyAMethod();进行限定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. 如果您将继承class A那么您的代码就可以了,因为类C也将继承该方法。

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(); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM