Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6893967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:43:27+00:00 2026-05-27T06:43:27+00:00

Let’s say I have, public class A { public static void MyAMethod() { }

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:43:27+00:00Added an answer on May 27, 2026 at 6:43 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have the following entity: public class Store { public List<Product> Products
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I can call a method like this: core::get() . What is the
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have some text as follows: do this, do that, then this,
Let's say I have table with column 'URL' whrere I store urls like this
Let's imagine I have a Java class of the type: public class MyClass {
Let's say that I have a set of relations that looks like this: relations

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.