[Mono-list] Reflection of non-public fields

Kornél Pál kornelpal at gmail.com
Thu Jun 29 18:07:57 EDT 2006


This is an instance field so you should use GetFields(BindingFlags.NonPublic 
| BindingFlags.Instance).

The field is not removed, that's why you get the warning so that the 
compiler warns you that you should probably remove the field if you are sure 
that you don't need it. You can check the presence of the field using tools 
like monodis, ildasm or Reflector.

Kornél

----- Original Message ----- 
From: <colin at breame.net>
To: <mono-list at lists.ximian.com>
Sent: Thursday, June 29, 2006 11:56 PM
Subject: [Mono-list] Reflection of non-public fields


> I'm not entirely sure I've got this right so I thought I'd run it past
> "the list" before I created a bug report.
>
> On running [1] I would expect to see "Hello World" but no output is
> produced.  When I compile I see a couple of warnings about the fields
> not being used - is the compiler being clever here and removing the
> fields entirely?
>
> -- Colin
>
> [1]:
> using System;
> using System.Reflection;
> public class test_t {
>  private string Hello;
>  private string World;
>  public static void Main() {
>    foreach (FieldInfo fi in
>             typeof(test_t).GetFields(BindingFlags.NonPublic)) {
>      Console.Write("{0} ", fi.Name);
>    }
>    Console.WriteLine();
>  }
> }
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list 



More information about the Mono-list mailing list