private Delegate GetDelegate(DelegateEntry de, int index) { Delegate delegate2; try { if ((de.methodName == null) || (de.methodName.Length == 0)) { this.ThrowInsufficientState("MethodName"); } if ((de.assembly == null) || (de.assembly.Length == 0)) { this.ThrowInsufficientState("DelegateAssembly"); } if ((de.targetTypeName == null) || (de.targetTypeName.Length == 0)) { this.ThrowInsufficientState("TargetTypeName"); } Type type = Assembly.Load(de.assembly).GetType(de.type, true, false); Type type2 = Assembly.Load(de.targetTypeAssembly).GetType(de.targetTypeName, true, false); if (this.m_methods != null) { object firstArgument = (de.target != null) ? RemotingServices.CheckCast(de.target, type2) : null; delegate2 = Delegate.InternalCreateDelegate(type, firstArgument, this.m_methods[index]); } else if (de.target != null) { delegate2 = Delegate.CreateDelegate(type, RemotingServices.CheckCast(de.target, type2), de.methodName); } else { delegate2 = Delegate.CreateDelegate(type, type2, de.methodName); } if (((delegate2.Method == null) || delegate2.Method.IsPublic) && ((delegate2.Method.DeclaringType == null) || delegate2.Method.DeclaringType.IsVisible)) { return delegate2; } new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand(); } catch (Exception exception) { if (exception is SerializationException) { throw exception; } throw new SerializationException(exception.Message, exception); } catch { throw new SerializationException(); } return delegate2; }
[System.Security.SecurityCritical] // auto-generated internal static DelegateEntry GetDelegateSerializationInfo( SerializationInfo info, Type delegateType, Object target, MethodInfo method, int targetIndex) { // Used for MulticastDelegate if (method == null) throw new ArgumentNullException("method"); Contract.EndContractBlock(); if (!method.IsPublic || (method.DeclaringType != null && !method.DeclaringType.IsVisible)) new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand(); Type c = delegateType.BaseType; if (c == null || (c != typeof(Delegate) && c != typeof(MulticastDelegate))) throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"),"type"); if (method.DeclaringType == null) throw new NotSupportedException(Environment.GetResourceString("NotSupported_GlobalMethodSerialization")); DelegateEntry de = new DelegateEntry(delegateType.FullName, delegateType.Module.Assembly.FullName, target, method.ReflectedType.Module.Assembly.FullName, method.ReflectedType.FullName, method.Name); if (info.MemberCount == 0) { info.SetType(typeof(DelegateSerializationHolder)); info.AddValue("Delegate",de,typeof(DelegateEntry)); } // target can be an object so it needs to be added to the info, or else a fixup is needed // when deserializing, and the fixup will occur too late. If it is added directly to the // info then the rules of deserialization will guarantee that it will be available when // needed if (target != null) { String targetName = "target" + targetIndex; info.AddValue(targetName, de.target); de.target = targetName; } // Due to a number of additions (delegate signature binding relaxation, delegates with open this or closed over the // first parameter and delegates over generic methods) we need to send a deal more information than previously. We can // get this by serializing the target MethodInfo. We still need to send the same information as before though (the // DelegateEntry above) for backwards compatibility. And we want to send the MethodInfo (which is serialized via an // ISerializable holder) as a top-level child of the info for the same reason as the target above -- we wouldn't have an // order of deserialization guarantee otherwise. String methodInfoName = "method" + targetIndex; info.AddValue(methodInfoName, method); return de; }
private DelegateSerializationHolder(SerializationInfo info, StreamingContext context) { if (info == null) { throw new ArgumentNullException("info"); } bool flag = true; try { this.m_delegateEntry = (DelegateEntry) info.GetValue("Delegate", typeof(DelegateEntry)); } catch { this.m_delegateEntry = this.OldDelegateWireFormat(info, context); flag = false; } if (flag) { DelegateEntry delegateEntry = this.m_delegateEntry; int num = 0; while (delegateEntry != null) { if (delegateEntry.target != null) { string target = delegateEntry.target as string; if (target != null) { delegateEntry.target = info.GetValue(target, typeof(object)); } } num++; delegateEntry = delegateEntry.delegateEntry; } MethodInfo[] infoArray = new MethodInfo[num]; int index = 0; while (index < num) { string name = "method" + index; infoArray[index] = (MethodInfo) info.GetValueNoThrow(name, typeof(MethodInfo)); if (infoArray[index] == null) { break; } index++; } if (index == num) { this.m_methods = infoArray; } } }
// Used for MulticastDelegate internal static DelegateEntry GetDelegateSerializationInfo(SerializationInfo info, Type delegateType, Object target, MethodInfo method, int targetIndex) { Message.DebugOut("Inside GetDelegateSerializationInfo \n"); if (method==null) { throw new ArgumentNullException("method"); } BCLDebug.Assert(!(target is Delegate),"!(target is Delegate)"); BCLDebug.Assert(info!=null, "[DelegateSerializationHolder.GetDelegateSerializationInfo]info!=null"); Type c = delegateType.BaseType; if (c == null || (c != typeof(Delegate) && c != typeof(MulticastDelegate))) { throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"),"type"); } DelegateEntry de = new DelegateEntry( delegateType.FullName, delegateType.Module.Assembly.FullName, target, method.ReflectedType.Module.Assembly.FullName, method.ReflectedType.FullName, method.Name ); if (info.MemberCount == 0) { info.SetType(typeof(DelegateSerializationHolder)); info.AddValue("Delegate",de,typeof(DelegateEntry)); } // target can be an object so it needs to be added to the info, or else a fixup is needed // when deserializing, and the fixup will occur too late. If it is added directly to the // info then the rules of deserialization will guarantee that it will be available when // needed if (target != null) { String targetName = "target"+targetIndex; info.AddValue(targetName, de.target); de.target = targetName; } return de; }
// Serialize a multicast delegate object. internal static void SerializeMulticast (SerializationInfo info, MulticastDelegate del) { DelegateEntry entry, next; int index = 0; // Serialize the first entry on the multicast list. entry = Serialize(info, index++, del.GetType(), del.target, MethodBase.GetMethodFromHandle(del.method)); // Serialize the rest of the multicast chain. del = del.prev; while(del != null) { next = Serialize (info, index++, del.GetType(), del.target, MethodBase.GetMethodFromHandle(del.method)); entry.delegateEntry = next; entry = next; del = del.prev; } }
// Serialize a delegate object. internal static DelegateEntry Serialize (SerializationInfo info, int listPosition, Type delegateType, Object target, MethodBase method) { // Create the new delegate entry block. DelegateEntry entry = new DelegateEntry(); entry.type = delegateType.FullName; entry.assembly = delegateType.Assembly.FullName; entry.target = target; entry.targetTypeAssembly = method.ReflectedType.Assembly.FullName; entry.targetTypeName = method.ReflectedType.FullName; entry.methodName = method.Name; // Add the block if this is the first in the list. if(info.MemberCount == 0) { info.SetType(typeof(DelegateSerializationHolder)); info.AddValue("Delegate", entry, typeof(DelegateEntry)); } // Add the target object to the top level of the info block. // Needed to get around order of fixup problems in some // third party serialization implementations. if(target != null) { String name = "target" + listPosition.ToString(); info.AddValue(name, target, typeof(Object)); entry.target = name; } // Return the entry to the caller so that we can chain // multiple entries together for multicast delegates. return entry; }
// Constructor. public DelegateSerializationHolder(SerializationInfo info, StreamingContext context) { if(info == null) { throw new ArgumentNullException("info"); } bool needsResolve = false; try { // Try the new serialization format first. entry = (DelegateEntry)(info.GetValue ("Delegate", typeof(DelegateEntry))); needsResolve = true; } catch(Exception) { // Try the old-style serialization format (deprecated). entry = new DelegateEntry(); entry.type = info.GetString("DelegateType"); entry.assembly = info.GetString("DelegateAssembly"); entry.target = info.GetValue("Target", typeof(Object)); entry.targetTypeAssembly = info.GetString("TargetTypeAssembly"); entry.targetTypeName = info.GetString("TargetTypeName"); entry.methodName = info.GetString("MethodName"); } // Resolve targets specified as field names. if(needsResolve) { DelegateEntry e = entry; while(e != null) { if(e.target is String) { e.target = info.GetValue ((String)(e.target), typeof(Object)); } e = e.delegateEntry; } } }
private Delegate GetDelegate(DelegateEntry de, int index) { Delegate d; try { if (de.methodName == null || de.methodName.Length == 0) ThrowInsufficientState("MethodName"); if (de.assembly == null || de.assembly.Length == 0) ThrowInsufficientState("DelegateAssembly"); if (de.targetTypeName == null || de.targetTypeName.Length == 0) ThrowInsufficientState("TargetTypeName"); // We cannot use Type.GetType directly, because of AppCompat - assembly names starting with '[' would fail to load. RuntimeType type = (RuntimeType)Assembly.GetType_Compat(de.assembly, de.type); RuntimeType targetType = (RuntimeType)Assembly.GetType_Compat(de.targetTypeAssembly, de.targetTypeName); // If we received the new style delegate encoding we already have the target MethodInfo in hand. if (m_methods != null) { #if FEATURE_REMOTING Object target = de.target != null ? RemotingServices.CheckCast(de.target, targetType) : null; #else if(!targetType.IsInstanceOfType(de.target)) throw new InvalidCastException(); Object target=de.target; #endif d = Delegate.CreateDelegateNoSecurityCheck(type, target, m_methods[index]); } else { if (de.target != null) #if FEATURE_REMOTING d = Delegate.CreateDelegate(type, RemotingServices.CheckCast(de.target, targetType), de.methodName); #else { if(!targetType.IsInstanceOfType(de.target)) throw new InvalidCastException(); d = Delegate.CreateDelegate(type, de.target, de.methodName); } #endif else d = Delegate.CreateDelegate(type, targetType, de.methodName); } if ((d.Method != null && !d.Method.IsPublic) || (d.Method.DeclaringType != null && !d.Method.DeclaringType.IsVisible)) new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand(); } catch (Exception e) { if (e is SerializationException) throw e; throw new SerializationException(e.Message, e); } return d; }
[System.Security.SecurityCritical] // auto-generated private DelegateSerializationHolder(SerializationInfo info, StreamingContext context) { if (info == null) throw new ArgumentNullException("info"); Contract.EndContractBlock(); bool bNewWire = true; try { m_delegateEntry = (DelegateEntry)info.GetValue("Delegate", typeof(DelegateEntry)); } catch { // Old wire format m_delegateEntry = OldDelegateWireFormat(info, context); bNewWire = false; } if (bNewWire) { // retrieve the targets DelegateEntry deiter = m_delegateEntry; int count = 0; while (deiter != null) { if (deiter.target != null) { string stringTarget = deiter.target as string; //need test to pass older wire format if (stringTarget != null) deiter.target = info.GetValue(stringTarget, typeof(Object)); } count++; deiter = deiter.delegateEntry; } // If the sender is as recent as us they'll have provided MethodInfos for each delegate. Look for these and pack // them into an ordered array if present. MethodInfo[] methods = new MethodInfo[count]; int i; for (i = 0; i < count; i++) { String methodInfoName = "method" + i; methods[i] = (MethodInfo)info.GetValueNoThrow(methodInfoName, typeof(MethodInfo)); if (methods[i] == null) break; } // If we got the info then make the array available for deserialization. if (i == count) m_methods = methods; } }
private Delegate GetDelegate(DelegateEntry de, int index) { Delegate d; try { if (de.methodName == null || de.methodName.Length == 0) ThrowInsufficientState("MethodName"); if (de.assembly == null || de.assembly.Length == 0) ThrowInsufficientState("DelegateAssembly"); if (de.targetTypeName == null || de.targetTypeName.Length == 0) ThrowInsufficientState("TargetTypeName"); Type type = Assembly.Load(de.assembly).GetType(de.type, true, false); Type targetType = Assembly.Load(de.targetTypeAssembly).GetType(de.targetTypeName, true, false); // If we received the new style delegate encoding we already have the target MethodInfo in hand. if (m_methods != null) { Object target = de.target != null ? RemotingServices.CheckCast(de.target, targetType) : null; d = Delegate.InternalCreateDelegate(type, target, m_methods[index]); } else { if (de.target != null) d = Delegate.CreateDelegate(type, RemotingServices.CheckCast(de.target, targetType), de.methodName); else d = Delegate.CreateDelegate(type, targetType, de.methodName); } if ((d.Method != null && !d.Method.IsPublic) || (d.Method.DeclaringType != null && !d.Method.DeclaringType.IsVisible)) new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand(); } catch (Exception e) { if (e is SerializationException) throw e; throw new SerializationException(e.Message, e); } catch { throw new SerializationException(); } return d; }
public static void GetDelegateData (Delegate instance, SerializationInfo info, StreamingContext ctx) { // Fills a SerializationInfo object with the information of the delegate. Delegate[] delegates = instance.GetInvocationList (); DelegateEntry lastEntry = null; for (int n=0; n<delegates.Length; n++) { Delegate del = delegates[n]; string targetLabel = (del.Target != null) ? ("target" + n) : null; DelegateEntry entry = new DelegateEntry (del, targetLabel); if (lastEntry == null) info.AddValue ("Delegate", entry); else lastEntry.delegateEntry = entry; lastEntry = entry; if (del.Target != null) info.AddValue (targetLabel, del.Target); } info.SetType (typeof (DelegateSerializationHolder)); }
internal static DelegateEntry GetDelegateSerializationInfo(SerializationInfo info, Type delegateType, object target, MethodInfo method, int targetIndex) { if (method == null) { throw new ArgumentNullException("method"); } if (!method.IsPublic || ((method.DeclaringType != null) && !method.DeclaringType.IsVisible)) { new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand(); } Type baseType = delegateType.BaseType; if ((baseType == null) || ((baseType != typeof(Delegate)) && (baseType != typeof(MulticastDelegate)))) { throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), "type"); } if (method.DeclaringType == null) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_GlobalMethodSerialization")); } DelegateEntry entry = new DelegateEntry(delegateType.FullName, delegateType.Module.Assembly.FullName, target, method.ReflectedType.Module.Assembly.FullName, method.ReflectedType.FullName, method.Name); if (info.MemberCount == 0) { info.SetType(typeof(DelegateSerializationHolder)); info.AddValue("Delegate", entry, typeof(DelegateEntry)); } if (target != null) { string str = "target" + targetIndex; info.AddValue(str, entry.target); entry.target = str; } string name = "method" + targetIndex; info.AddValue(name, method); return entry; }
internal DelegateSerializationHolder(SerializationInfo info, StreamingContext context) { if (info==null) { throw new ArgumentNullException("info"); } bool bNewWire = true; try { m_delegateEntry = (DelegateEntry)info.GetValue("Delegate", typeof(DelegateEntry)); } catch(Exception) { // Old wire format m_delegateEntry = OldDelegateWireFormat(info, context); bNewWire = false; } if (bNewWire) { // retrieve the targets DelegateEntry deiter = m_delegateEntry; while (deiter != null) { if (deiter.target != null) { string stringTarget = deiter.target as string; //need test to pass older wire format if (stringTarget != null) deiter.target = info.GetValue(stringTarget, typeof(Object)); } deiter= deiter.delegateEntry; } } }
private Delegate GetDelegate(DelegateEntry de) { Delegate d; if (de.methodName==null || de.methodName.Length==0) { throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientDeserializationState")); } Assembly assem = FormatterServices.LoadAssemblyFromString(de.assembly); if (assem==null) { BCLDebug.Trace("SER", "[DelegateSerializationHolder.ctor]: Unable to find assembly for TargetType: ", de.assembly); throw new SerializationException(String.Format(Environment.GetResourceString("Serialization_AssemblyNotFound"), de.assembly)); } Type type = assem.GetTypeInternal(de.type, false, false, false); assem = FormatterServices.LoadAssemblyFromString(de.targetTypeAssembly); if (assem==null) { BCLDebug.Trace("SER", "[DelegateSerializationHolder.ctor]: Unable to find assembly for TargetType: ", de.targetTypeAssembly); throw new SerializationException(String.Format(Environment.GetResourceString("Serialization_AssemblyNotFound"), de.targetTypeAssembly)); } Type targetType = assem.GetTypeInternal(de.targetTypeName, false, false, false); if (de.target==null && targetType==null) { throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientDeserializationState")); } if (type==null) { BCLDebug.Trace("SER","[DelegateSerializationHolder.GetRealObject]Missing Type"); throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientDeserializationState")); } if (targetType==null) { throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientDeserializationState")); } Object target = null; if (de.target!=null) { //We have a delegate to a non-static object target = RemotingServices.CheckCast(de.target, targetType); d=Delegate.CreateDelegate(type, target, de.methodName); } else { //For a static delegate d=Delegate.CreateDelegate(type, targetType, de.methodName); } // We will refuse to create delegates to methods that are non-public. MethodInfo mi = d.Method; if (mi != null) { if (!mi.IsPublic) { throw new SerializationException( Environment.GetResourceString("Serialization_RefuseNonPublicDelegateCreation")); } } return d; }