Type.GetFields Method (BindingFlags)
-
Hello everyone, From MSDN about Type.GetFields Method (BindingFlags), http://msdn.microsoft.com/en-us/library/6ztex2dc(VS.80).aspx What means "When overridden in a derived class, searches for the fields defined for the current Type"? I can not find related description for this point in the MSDN related sample. Especially "overridden in a derived class" and "defined for the current Type"? thanks in advance, George
-
Hello everyone, From MSDN about Type.GetFields Method (BindingFlags), http://msdn.microsoft.com/en-us/library/6ztex2dc(VS.80).aspx What means "When overridden in a derived class, searches for the fields defined for the current Type"? I can not find related description for this point in the MSDN related sample. Especially "overridden in a derived class" and "defined for the current Type"? thanks in advance, George
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 fromType
, 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
-
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 fromType
, 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
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
-
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
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
-
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
Thanks Senthil, The fields only for member variables, not for functions? regards, 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
-
Thanks Senthil, The fields only for member variables, not for functions? regards, George
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
-
Thanks Senthil, The fields only for member variables, not for functions? regards, George
-
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
-
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)For any user defined class, does the class implicitly derives from Type? regards, George
-
Call GetMembers then.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)I have tested it, leppie! Looks like GetMembers is more powerful than GetFields. :-) regards, George
-
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
Thanks Senthil, Looks like GetMembers return both methods and fields, and GetMethods/GetFields returns just method/field? So, GetMembers is more powerful. :-) regards, George
-
The field abc is defined as static, but in your GetFields() call you ask it to return only instance fields.
Thanks Vega02, I think the root cause is, I do not know GetFields will not return methods before. :-) regards, George