|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Type.GetFields change from 1.1 to 2.0I have some code that worked fine with .NET 1.1 but something has changed in
2.0 that I don't see documented. I have the line: foreach (FieldInfo field in type.GetFields()) But there are never any fields returned like there was with 1.1. With the help if ildasm I tried the following and it returns some properties. foreach (PropertyInfo property in type.GetProperties()) One other problem is that when I call GetMembers and look at each of the MemberType it seems that none are FieldType anymore. Any ideas? Am I just doing something wrong? Kevin Kevin Burton <KevinBur***@discussions.microsoft.com> wrote:
> I have some code that worked fine with .NET 1.1 but something has changed in What is 'type' the type object for?> 2.0 that I don't see documented. I have the line: > > foreach (FieldInfo field in type.GetFields()) > But there are never any fields returned like there was with 1.1. With the Is it that your fields are non-public? GetFields() doesn't return> help if ildasm I tried the following and it returns some properties. > One other problem is that when I call GetMembers and look at each of the > MemberType it seems that none are FieldType anymore. Any ideas? Am I just > doing something wrong? private fields. You need to use the GetFields(BindingFlags) overload, and pass BindingFlags.NonPublic or'd with BindingFlags.Instance and/or BindingFlags.Static to get private fields. This code works fine: ---8<--- using System; using System.Reflection; class App { static int _staticField; int _instanceField; static void Main(string[] args) { foreach (FieldInfo f in typeof(App).GetFields( BindingFlags.NonPublic | BindingFlags.Instance Console.WriteLine(f.Name);| BindingFlags.Static)) } } --->8--- -- Barry What class are you trying to reflect? It sounds like the class now has
properties instead of fields. Cheers, Greg Young MVP - C# Show quote "Kevin Burton" <KevinBur***@discussions.microsoft.com> wrote in message news:F1DDFCF3-0B7F-43C5-8D04-1ECAE5586546@microsoft.com... >I have some code that worked fine with .NET 1.1 but something has changed >in > 2.0 that I don't see documented. I have the line: > > foreach (FieldInfo field in type.GetFields()) > > But there are never any fields returned like there was with 1.1. With the > help if ildasm I tried the following and it returns some properties. > > foreach (PropertyInfo property in type.GetProperties()) > > One other problem is that when I call GetMembers and look at each of the > MemberType it seems that none are FieldType anymore. Any ideas? Am I just > doing something wrong? > > Kevin > |
|||||||||||||||||||||||