private static void SerializeDelegate(SerializeContext context, BinaryWriter bw, Delegate d, Type delegateType) { Debug.Assert(d != null); Debug.Assert(StaticDelegateTable.delegateTypeTable.ContainsKey(delegateType)); var delegateTypeInfo = StaticDelegateTable.delegateTypeTable[delegateType]; var methodId = delegateTypeInfo.GetIdForMethod(d.Method); bw.Write(methodId); var delegateMethodInfo = delegateTypeInfo.GetMethodInfoForId(methodId); if (delegateMethodInfo.canHaveTarget) { var target = d.Target; if (context.Walk(target)) { StaticDispatchTable.SerializationDispatcher(context, bw, target); } } else { Debug.Assert(d.Target == null); } }
private static void DeserializeDelegate(DeserializeContext context, BinaryReader br, ref Delegate d, Type delegateType) { Debug.Assert(StaticDelegateTable.delegateTypeTable.ContainsKey(delegateType)); var delegateTypeInfo = StaticDelegateTable.delegateTypeTable[delegateType]; var delegateMethodInfo = delegateTypeInfo.GetMethodInfoForId(br.ReadInt32()); object target = null; if (delegateMethodInfo.canHaveTarget) { if (context.Walk(ref target)) { target = StaticDispatchTable.DeserializationDispatcher(context, br); // (see comment in the SerializeDelegate method) } } d = Delegate.CreateDelegate(delegateType, target, delegateMethodInfo.method); }