1
votes

I'm probably doing something silly, but here it goes.

I'm trying to get the FieldInfo from a public event via reflection.

Check this function:

  public void PlotAllFields(Type type) {
      BindingFlags all = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
      FieldInfo[] fields = type.GetFields(all);
      Console.WriteLine(type + "-------------------------");
      foreach (var fieldInfo in fields) {
          Console.WriteLine(fieldInfo.Name);
      }
  }

  public class Bar : Foo {}

  public class Foo {
      public string Test;
      public event EventHandler Event;
      public event RoutedEventHandler RoutedEvent;
  }

The call PlotAllFields(typeof(Foo)); returns:

  • Test
  • Event
  • RoutedEvent

The call PlotAllFields(typeof(Bar)); returns:

  • Test

I understand that the delegates behind the events are private fields so I can't access them on the subclass. So far so good.

Then I tried: PlotAllFields(typeof(FrameworkElement)); //from WPF

  • _themeStyleCache
  • _styleCache
  • _templatedParent
  • _templateChild
  • _flags
  • _flags2
  • _parent
  • _inheritableProperties
  • MeasureRequest
  • ArrangeRequest
  • sizeChangedInfo
  • _parentIndex
  • _parent
  • _proxy
  • _contextStorage

Well... Where are the 14 events of FrameworkElement class???

2

2 Answers

2
votes

FrameworkElement doesn't use field-like events, it makes calls to AddHandler and RemoveHandler. Most of the time they don't have handlers attached, so WPF uses a system that is more space-efficient. For example, here is the Loaded event, from Reflector:

public event RoutedEventHandler Loaded
{
    add
    {
        base.AddHandler(LoadedEvent, value, false);
    }
    remove
    {
        base.RemoveHandler(LoadedEvent, value);
    }
}
0
votes