Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Type.GetFields Method (BindingFlags)

Type.GetFields Method (BindingFlags)

Scheduled Pinned Locked Moved C#
visual-studiocomquestion
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S S Senthil Kumar

    George_George wrote:

    What means "When overridden in a derived class, searches for the fields defined for the current Type"?

    System.Type is an abstract class, and GetFields is an abstract method. There are several classes that derived from Type, and MSDN says that when it is overridden in a derived class, it must return the fields associated with the "underlying" type that the Type object represents.

    Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

    G Offline
    G Offline
    George_George
    wrote on last edited by
    #3

    Thanks Senthil, I have written a test case, I am using a base class type variable to point to a derived type and in the derived type, I override a method of base class. But it seems I can get nothing in variable fields. Any ideas?

    class Program
    {
        class Foo
        {
            public static int abc;
            virtual public void bcd()
            {
    
            }
        }
    
        class Goo : Foo
        {
            override public void bcd()
            {
    
            }
        }
    
        static void Main(string\[\] args)
        {
            Foo f = new Goo();
            FieldInfo\[\] fields = f.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
    
            return;
        }
    }
    

    regards, George

    S V 2 Replies Last reply
    0
    • G George_George

      Thanks Senthil, I have written a test case, I am using a base class type variable to point to a derived type and in the derived type, I override a method of base class. But it seems I can get nothing in variable fields. Any ideas?

      class Program
      {
          class Foo
          {
              public static int abc;
              virtual public void bcd()
              {
      
              }
          }
      
          class Goo : Foo
          {
              override public void bcd()
              {
      
              }
          }
      
          static void Main(string\[\] args)
          {
              Foo f = new Goo();
              FieldInfo\[\] fields = f.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
      
              return;
          }
      }
      

      regards, George

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #4

      George_George wrote:

      But it seems I can get nothing in variable fields. Any ideas?

      The text in the MSDN page is for people deriving from System.Type, for whatever reason. From a user's perspective, it returns the list of fields in the class represented by the Type.

      Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

      G L 2 Replies Last reply
      0
      • S S Senthil Kumar

        George_George wrote:

        But it seems I can get nothing in variable fields. Any ideas?

        The text in the MSDN page is for people deriving from System.Type, for whatever reason. From a user's perspective, it returns the list of fields in the class represented by the Type.

        Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #5

        Thanks Senthil, The fields only for member variables, not for functions? regards, George

        S L 2 Replies Last reply
        0
        • G George_George

          Thanks Senthil, I have written a test case, I am using a base class type variable to point to a derived type and in the derived type, I override a method of base class. But it seems I can get nothing in variable fields. Any ideas?

          class Program
          {
              class Foo
              {
                  public static int abc;
                  virtual public void bcd()
                  {
          
                  }
              }
          
              class Goo : Foo
              {
                  override public void bcd()
                  {
          
                  }
              }
          
              static void Main(string\[\] args)
              {
                  Foo f = new Goo();
                  FieldInfo\[\] fields = f.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
          
                  return;
              }
          }
          

          regards, George

          V Offline
          V Offline
          Vega02
          wrote on last edited by
          #6

          The field abc is defined as static, but in your GetFields() call you ask it to return only instance fields.

          G 1 Reply Last reply
          0
          • G George_George

            Thanks Senthil, The fields only for member variables, not for functions? regards, George

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #7

            GetFields returns only fields (member variables) in that type. Type.GetMethods[^] returns functions defined in that Type.

            Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

            G 1 Reply Last reply
            0
            • G George_George

              Thanks Senthil, The fields only for member variables, not for functions? regards, George

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #8

              Call GetMembers then.

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 alpha 4a out now (29 May 2008)

              G 1 Reply Last reply
              0
              • S S Senthil Kumar

                George_George wrote:

                But it seems I can get nothing in variable fields. Any ideas?

                The text in the MSDN page is for people deriving from System.Type, for whatever reason. From a user's perspective, it returns the list of fields in the class represented by the Type.

                Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #9

                S. Senthil Kumar wrote:

                for whatever reason.

                Every single type defined derives from Type as a RuntimeType.

                xacc.ide - now with TabsToSpaces support
                IronScheme - 1.0 alpha 4a out now (29 May 2008)

                G 1 Reply Last reply
                0
                • L leppie

                  S. Senthil Kumar wrote:

                  for whatever reason.

                  Every single type defined derives from Type as a RuntimeType.

                  xacc.ide - now with TabsToSpaces support
                  IronScheme - 1.0 alpha 4a out now (29 May 2008)

                  G Offline
                  G Offline
                  George_George
                  wrote on last edited by
                  #10

                  For any user defined class, does the class implicitly derives from Type? regards, George

                  1 Reply Last reply
                  0
                  • L leppie

                    Call GetMembers then.

                    xacc.ide - now with TabsToSpaces support
                    IronScheme - 1.0 alpha 4a out now (29 May 2008)

                    G Offline
                    G Offline
                    George_George
                    wrote on last edited by
                    #11

                    I have tested it, leppie! Looks like GetMembers is more powerful than GetFields. :-) regards, George

                    1 Reply Last reply
                    0
                    • S S Senthil Kumar

                      GetFields returns only fields (member variables) in that type. Type.GetMethods[^] returns functions defined in that Type.

                      Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

                      G Offline
                      G Offline
                      George_George
                      wrote on last edited by
                      #12

                      Thanks Senthil, Looks like GetMembers return both methods and fields, and GetMethods/GetFields returns just method/field? So, GetMembers is more powerful. :-) regards, George

                      1 Reply Last reply
                      0
                      • V Vega02

                        The field abc is defined as static, but in your GetFields() call you ask it to return only instance fields.

                        G Offline
                        G Offline
                        George_George
                        wrote on last edited by
                        #13

                        Thanks Vega02, I think the root cause is, I do not know GetFields will not return methods before. :-) regards, George

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups