Type info in static method

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

    Type info in static method

    How do i get the type info in a static method?

    for instance in the code below is it possible for the Method to get the type
    to know what type was used to call Method?

    thanks



    class C {
    public C() {
    }

    public static Method() {
    }
    }

    Class D : C {
    }

    C c = new C();
    D d = new D();
    d.Method();
    c.cMethod;


  • Mattias Sjögren

    #2
    Re: Type info in static method

    >How do i get the type info in a static method?
    Only with typeof(YourClas s). Since it's a static method there's no
    'this' reference to get type info from.

    >for instance in the code below is it possible for the Method to get the type
    >to know what type was used to call Method?
    That code shouldn't even compile since you can't call static methods
    on instances. But if you write

    D.Method();

    it will still compile as

    C.Method();

    so there's no way for the code in Method to tell the difference.


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • ssamuel

      #3
      Re: Type info in static method

      You know the type info in a static method, so there's no reason to get
      it from anywhere. Static methods are contained and runnable only in a
      given type, so the current type, and therefore the type info, is known
      at design time and fixed.

      In your example, you seem to want to know if you're calling Method() on
      C or D. The fact is that you're always and only calling it on C,
      because there is no D.Method() per se. D appears to have a static
      Method() because it inherited it from its base, C, but you're still
      calling it on C, not D.

      If you want D.Method() to do something different from C.Method(),
      create the following:

      class D : C
      {
      public new static void Method() { /* do something different */ }
      }

      As noted elsewhere, this is a good example of why you should try to
      compile any code that you post before you post it. If you'd done that,
      you would have noticed several errors in your code that probably would
      have tipped you off to the answer.


      Stephan



      mfc wrote:
      How do i get the type info in a static method?
      >
      for instance in the code below is it possible for the Method to get the type
      to know what type was used to call Method?
      >
      thanks
      >
      >
      >
      class C {
      public C() {
      }
      >
      public static Method() {
      }
      }
      >
      Class D : C {
      }
      >
      C c = new C();
      D d = new D();
      d.Method();
      c.cMethod;

      Comment

      • mfc

        #4
        Re: Type info in static method


        "ssamuel" <ssamuel@gmail. comwrote in message
        news:1164672079 .718273.111130@ f16g2000cwb.goo glegroups.com.. .
        You know the type info in a static method, so there's no reason to get
        it from anywhere. Static methods are contained and runnable only in a
        given type, so the current type, and therefore the type info, is known
        at design time and fixed.
        >
        Well, I guess thats a limitation. I can think of many reasons why I would
        want to know the type in a static method and won't know the type at compile
        time. For instance suppose I wanted a class that contains data for a
        listview that could query itself using reflection and find all its public
        properties and then use that information to generate the column headers of a
        listview. At the initialization stage when its setting up the UI, there
        won't be an instance of the class since it hasn't hit the database yet, but
        the class inherently contains all the information needed to set up the
        coloumn headers and can do so via a static method. But the problem is that
        any classes based on this class doesn't know what its type is unless its
        passed in


        public abstract class ListViewData{
        public ListViewData() {
        }

        public abstract object this[string index] {
        get;
        set;
        }

        /// <summary>
        /// Sets up the column headers for the ListView
        /// </summary>
        public static void SetListViewColu mnHeaders(Type type,
        ListView.Column HeaderCollectio n headers) {
        PropertyInfo[] infoList = type.GetPropert ies(BindingFlag s.Public |
        BindingFlags.In stance);

        foreach (PropertyInfo p in infoList) {
        foreach (Attribute attribute in Attribute.GetCu stomAttributes( p)) {
        GridViewColumnA ttribute a = (GridViewColumn Attribute) attribute;

        if (a == null || a.Include == false)
        continue;

        headers.Add(a.N ame, a.Width).Tag = new GridViewColumnT ag(p.Name,
        p.PropertyType, a.Flags);
        }
        }
        }
        }
        In your example, you seem to want to know if you're calling Method() on
        C or D. The fact is that you're always and only calling it on C,
        because there is no D.Method() per se. D appears to have a static
        Method() because it inherited it from its base, C, but you're still
        calling it on C, not D.
        >

        Comment

        • Joanna Carter [TeamB]

          #5
          Re: Type info in static method

          "mfc" <mfc_mobile@new sgroup.nospama écrit dans le message de news:
          uriX2joEHHA.462 0@TK2MSFTNGP04. phx.gbl...
          | For instance suppose I wanted a class that contains data for a
          | listview that could query itself using reflection and find all its public
          | properties and then use that information to generate the column headers of
          a
          | listview. At the initialization stage when its setting up the UI, there
          | won't be an instance of the class since it hasn't hit the database yet,
          but
          | the class inherently contains all the information needed to set up the
          | coloumn headers and can do so via a static method. But the problem is that
          | any classes based on this class doesn't know what its type is unless its
          | passed in

          But surely that is what you already have with the following :

          {
          PropertyInfo[] properties = typeof(MyType). GetProperties(. ..);

          ...
          }

          Joanna

          --
          Joanna Carter [TeamB]
          Consultant Software Engineer


          Comment

          Working...