Home All Groups Group Topic Archive Search About

Type.GetFields change from 1.1 to 2.0

Author
27 May 2006 6:53 PM
Kevin Burton
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

Author
27 May 2006 7:40 PM
Barry Kelly
Kevin Burton <KevinBur***@discussions.microsoft.com> wrote:

> 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())

What is 'type' the type object for?

> 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.

> 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?

Is it that your fields are non-public? GetFields() doesn't return
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
                | BindingFlags.Static))
            Console.WriteLine(f.Name);
    }
}
--->8---

-- Barry

Author
27 May 2006 9:24 PM
Greg Young
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
>

AddThis Social Bookmark Button