locked
Calling static method of a derived class inside static method of the base class RRS feed

  • Question

  • Hi all,

    I have a problem with a static method.

    I have a baseClass and some derivedClasses. The baseClass has a static metod (staticBase), each derivedClass has a static method too (staticDerived).

    Calling staticBase by derivedClass, it's easy to know the caller class type (I use typeof), but how can I invoke the staticDerived method (inside the staticBase method) ?


    • Edited by Nazza Friday, May 25, 2012 7:28 AM
    Friday, May 25, 2012 7:27 AM

Answers

  • I did it !

    Type t = typeof(T);
    PropertyInfo pi = t.GetProperty("Table");
    string table = pi.GetValue(null, null).ToString();



    • Edited by Nazza Friday, May 25, 2012 9:48 AM
    • Marked as answer by Nazza Friday, May 25, 2012 9:48 AM
    Friday, May 25, 2012 9:47 AM

All replies

  • Hi, 

    "Calling staticBase by derivedClassAre you saying same from StaticDerived -> StaticBase -> StaticDerived? this causes cyclic issue.

    anyhow, You can call static method from base class as DerivedClass.StaticDerived() right? Please provide more information on problem.



    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Friday, May 25, 2012 7:52 AM
  • Base class can only call the static method of the derived class if the static method is public or internal. So you need to make static method in derived class public or internal and then you can call it inside the static method of the base class.

    If you need to restrict the visibility then set it internal. Then only classes in same assembly can call the static method in derived class and you have better control who actually is calling the method. Public visibility will make it visible to everybody.

    Friday, May 25, 2012 7:59 AM
  • Hi,

    normally, i would say: make a virtual staticDerived method with an empty implementation, and make an override in the derived class,

    but apparently, you can't have virtual static methods,

    so you'd have to make your methods non-static.

    It's well explained in this thread: http://stackoverflow.com/questions/763344/c-sharp-virtual-or-abstract-static-methods


    Regards, Nico

    Friday, May 25, 2012 8:02 AM
  • My code:

    // BaseClass public abstract class BaseClass { public static List<T> LoadValues<T>() where T : BaseClass, new() { // Here I have to call static property or method of calling class // Some code to load values from DB } } // DerivedClass public class DerivedClass : BaseClass { public static string Table { get { return "XYZ"; } } }

    // I have DerivedClass2, DerivedClass3, etc..

    // Calling List<DerivedClass> _list = DerivedClass.LoadValues<DerivedClass>();


    Here I presented a static property, but a static method is the same for my purpose.

    Inside LoadValues, it's easy to get the calling class type (typeof), but I don't know how to access "Table" property.

    Normally I write DerivedClass.Table, but if I have to determine wich class is the invoking one ?

    Friday, May 25, 2012 8:45 AM
  • I did it !

    Type t = typeof(T);
    PropertyInfo pi = t.GetProperty("Table");
    string table = pi.GetValue(null, null).ToString();



    • Edited by Nazza Friday, May 25, 2012 9:48 AM
    • Marked as answer by Nazza Friday, May 25, 2012 9:48 AM
    Friday, May 25, 2012 9:47 AM