Find current class from within a static method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Mott

    Find current class from within a static method

    Hi all,

    Is there a way to get the name of the current class in a static method? I
    have a base class that contains static methods and that class is inherited.
    I'd like to change the behavior in the derived class but static methods
    cannot be overridden. If I can find out 'who i am' i can do what I need to
    do.

    thanks,

    john


  • Jon Skeet [C# MVP]

    #2
    Re: Find current class from within a static method

    John Mott <johnmott59@hot mail.comwrote:
    Is there a way to get the name of the current class in a static method? I
    have a base class that contains static methods and that class is inherited.
    I'd like to change the behavior in the derived class but static methods
    cannot be overridden. If I can find out 'who i am' i can do what I need to
    do.
    I'm afraid not - there's no real idea of the "current class" in the
    context of a static method.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • =?Utf-8?B?TW9kZWxCdWlsZGVy?=

      #3
      RE: Find current class from within a static method

      Is the following similar to what you wish to achieve, such that
      Animal.LegCount and Lion.LegCount return differing logic?

      public class Animal
      {
      public static int LegCount()
      {
      return 0;
      }
      }

      public class Lion : Animal
      {
      public new static int LegCount()
      {
      return 4;
      }
      }


      "John Mott" wrote:
      Hi all,
      >
      Is there a way to get the name of the current class in a static method? I
      have a base class that contains static methods and that class is inherited.
      I'd like to change the behavior in the derived class but static methods
      cannot be overridden. If I can find out 'who i am' i can do what I need to
      do.
      >
      thanks,
      >
      john
      >
      >
      >

      Comment

      • John Mott

        #4
        Re: Find current class from within a static method


        That is defintely an improvement over what I have now. I have multiple
        static routines but this scheme could reduce the number requires in the
        derived class to just what it has to 'know', then it can manually call the
        base class routines.

        thanks!

        john

        "ModelBuild er" <ModelBuilder@d iscussions.micr osoft.comwrote in message
        news:0C94BED3-27A7-4DFD-891C-76A20BA41981@mi crosoft.com...
        Is the following similar to what you wish to achieve, such that
        Animal.LegCount and Lion.LegCount return differing logic?
        >
        public class Animal
        {
        public static int LegCount()
        {
        return 0;
        }
        }
        >
        public class Lion : Animal
        {
        public new static int LegCount()
        {
        return 4;
        }
        }
        >
        >
        "John Mott" wrote:
        >
        >Hi all,
        >>
        >Is there a way to get the name of the current class in a static method? I
        >have a base class that contains static methods and that class is
        >inherited.
        >I'd like to change the behavior in the derived class but static methods
        >cannot be overridden. If I can find out 'who i am' i can do what I need
        >to
        >do.
        >>
        >thanks,
        >>
        >john
        >>
        >>
        >>

        Comment

        • Carlos S

          #5
          Re: Find current class from within a static method

          I would suggest going away from static methods in this case since what you're looking for is polymorphic behavior. I would make the method an instance method and mark it as virtual in the base class and override it in the subclass.

          Carlos S

          Comment

          • John Mott

            #6
            Re: Find current class from within a static method

            I'm with you on the behavior and many of the current methods are virtual and
            overridden for the instantiated objects, but its syntactically and logically
            convenient to use static methods -- they retrieve instances of the class
            through a query, so they are operating like a factory:

            DataTable dt = Prospects.Retri eve("state = 'TN'"); // retrieve all
            Prospects where the State is Tennessee

            Right now the static methods are declared on the derived classes, but I can
            push these into the base class and just pass in minor information. The goal
            is tidyness, more logic in the common base class and less in the derived
            class.

            john


            <Carlos Swrote in message news:upuh4rl7HH A.536@TK2MSFTNG P06.phx.gbl...
            >I would suggest going away from static methods in this case since what
            >you're looking for is polymorphic behavior. I would make the method an
            >instance method and mark it as virtual in the base class and override it in
            >the subclass.
            >
            Carlos S

            Comment

            • Doug Forster

              #7
              Re: Find current class from within a static method

              Hi John,
              Right now the static methods are declared on the derived classes, but I
              can push these into the base class and just pass in minor information. The
              goal is tidyness, more logic in the common base class and less in the
              derived class.
              Have you looked at custom attributes? A very neat way of adding static meta
              data to a class. Then you just need to pass the type into the base class
              static methods and they get the rest from the custom attributes.

              Cheers
              Doug Forster


              Comment

              • Stanimir Stoyanov

                #8
                Re: Find current class from within a static method

                [1] StackTrace reference is available at
                http://msdn2.microsoft.com/en-us/lib...tacktrace.aspx.

                Best Regards,
                Stanimir Stoyanov
                www.stoyanoff.info | www.aeroxp.org

                Comment

                • John Mott

                  #9
                  Re: Find current class from within a static method

                  wow. That looks promising. Thanks!

                  john

                  "Doug Forster" <nobody@nowhere ,comwrote in message
                  news:eycYQFr7HH A.5984@TK2MSFTN GP04.phx.gbl...
                  Hi John,
                  >
                  >Right now the static methods are declared on the derived classes, but I
                  >can push these into the base class and just pass in minor information.
                  >The goal is tidyness, more logic in the common base class and less in the
                  >derived class.
                  >
                  Have you looked at custom attributes? A very neat way of adding static
                  meta data to a class. Then you just need to pass the type into the base
                  class static methods and they get the rest from the custom attributes.
                  >
                  Cheers
                  Doug Forster
                  >

                  Comment

                  Working...