Reflections and private fields of event delegates in C #

I’m probably doing something stupid, but here it is.

I am trying to get FieldInfo from a public event through reflection.

Check out this feature:

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; } 

Call PlotAllFields (typeof (Foo)); returns:

  • Test
  • Event
  • RoutedEvent

Call PlotAllFields (typeof (Bar)); returns:

  • Test

I understand that delegates behind events are private fields, so I cannot access them in a 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 the FrameworkElement class ???

+1
source share
2 answers

FrameworkElement does not use field events; it calls AddHandler and RemoveHandler . In most cases, they do not have handlers, so WPF uses a more economical system. For example, here is the Loaded event, from Reflector:

 public event RoutedEventHandler Loaded { add { base.AddHandler(LoadedEvent, value, false); } remove { base.RemoveHandler(LoadedEvent, value); } } 
+2
source

try these anchor flags

 BindingFlags.Default | BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public 

http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(SYSTEM.REFLECTION.BINDINGFLAGS);k(TargetFrameworkMoniker-%22.NETFRAMEWORK,VERSION%3dV3.5%22) ; k (DevLang-CSHARP) & rd = true

0
source

All Articles