public SerializeDelegate(SerializationInfo info, StreamingContext context) { Type delType = (Type)info.GetValue("delegateType", typeof(Type)); //If it's a "simple" delegate we just read it straight off if (info.GetBoolean("isSerializable")) { Delegate = (Delegate)info.GetValue("delegate", delType); } //otherwise, we need to read its anonymous class else { MethodInfo method = (MethodInfo)info.GetValue("method", typeof(MethodInfo)); AnonymousClassWrapper w = (AnonymousClassWrapper)info.GetValue("class", typeof(AnonymousClassWrapper)); Delegate = Delegate.CreateDelegate(delType, w.obj, method); } }
/// <summary> /// Creates a new instance of <see cref="WebApplications.Utilities.Serialization.DelegateSerializer"/> /// </summary> /// <param name="info"> /// <para>The <see cref="System.Runtime.Serialization.SerializationInfo"/>.</para> /// <para>This object stores all the required information for serializing/deserializing the delegate.</para> /// </param> /// <param name="context"> /// The <see cref="System.Runtime.Serialization.StreamingContext"/> for this serialization. /// </param> /// <example> /// <code>formatter.Serialize(stream, new DelegateSerializer(info, context));</code> /// </example> // ReSharper disable once NotNullMemberIsNotInitialized - In the case of combined delegates, _delegate will be set in OnDeserialize internal DelegateSerializer([NotNull] SerializationInfo info, StreamingContext context) { if (info == null) { throw new ArgumentNullException("info"); } Type delType = (Type)info.GetValue(DelegateTypeName, typeof(Type)); Debug.Assert(delType != null); // If the delegate is a combined delegate, we need to read the delegates it contains if (info.GetBoolean(IsCombinedName)) { _delegateSerializers = (DelegateSerializer[])info.GetValue(DelegatesName, typeof(DelegateSerializer[])); Debug.Assert(_delegateSerializers != null); } else { // If it's a "simple" delegate we just read it straight off if (info.GetBoolean(IsSerializableName)) { Delegate @delegate = (Delegate)info.GetValue(DelegateName, delType); Debug.Assert(@delegate != null); _delegate = @delegate; } // Otherwise, we need to read its anonymous class else { MethodInfo method = (MethodInfo)info.GetValue(MethodName, typeof(MethodInfo)); Debug.Assert(method != null); AnonymousClassWrapper w = (AnonymousClassWrapper)info.GetValue(ClassName, typeof(AnonymousClassWrapper)); Debug.Assert(w != null); _delegate = Delegate.CreateDelegate(delType, w.Obj, method); } } }