lundi 14 mars 2016

Why can't reflection access the _invocationList/_invocationCount fields in a MulticastDelegate variable?

I'm typing all this in the immediate window while sitting on a breakpoint:

typeof(MulticastDelegate).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
{System.Reflection.FieldInfo[6]}
    [0]: {System.Object _invocationList}
    [1]: {IntPtr _invocationCount}
    [2]: {System.Object _target}
    [3]: {System.Object _methodBase}
    [4]: {IntPtr _methodPtr}
    [5]: {IntPtr _methodPtrAux}

Looks good, right? But let's try it with an actual delegate variable of type EventHandler (that has two registered listeners):

l is MulticastDelegate
true
l.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
{System.Reflection.FieldInfo[4]}
    [0]: {System.Object _target}
    [1]: {System.Object _methodBase}
    [2]: {IntPtr _methodPtr}
    [3]: {IntPtr _methodPtrAux}

And for good measure:

((MulticastDelegate)l).GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
{System.Reflection.FieldInfo[4]}
    [0]: {System.Object _target}
    [1]: {System.Object _methodBase}
    [2]: {IntPtr _methodPtr}
    [3]: {IntPtr _methodPtrAux}

Why can't I access _invocationList or _invocationCount by reflecting on the variable when I can by reflecting on MulticastDelegate directly?





Aucun commentaire:

Enregistrer un commentaire