System.DelegateSerializationHolder.GetDelegateSerializationInfo C# (CSharp) Méthode

GetDelegateSerializationInfo() static private méthode

static private GetDelegateSerializationInfo ( SerializationInfo info, Type delegateType, Object target, MethodInfo method, int targetIndex ) : DelegateEntry
info System.Runtime.Serialization.SerializationInfo
delegateType Type
target Object
method System.Reflection.MethodInfo
targetIndex int
Résultat DelegateEntry
        internal static DelegateEntry GetDelegateSerializationInfo(
            SerializationInfo info, Type delegateType, Object target, MethodInfo method, int targetIndex)
        {
            // Used for MulticastDelegate

            if (method == null) 
                throw new ArgumentNullException("method");
    
            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;
        }
        #endregion

Usage Example

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            int targetIndex = 0;

            Object[] invocationList = _invocationList as Object[];
            if (invocationList == null)
            {
                MethodInfo method = Method;
                // A MethodInfo object can be a RuntimeMethodInfo, a RefEmit method (MethodBuilder, etc), or a DynamicMethod
                // One can only create delegates on RuntimeMethodInfo and DynamicMethod.
                // If it is not a RuntimeMethodInfo (must be a DynamicMethod) or if it is an unmanaged function pointer, throw
                if (!(method is RuntimeMethodInfo) || IsUnmanagedFunctionPtr())
                {
                    throw new SerializationException(Environment.GetResourceString("Serialization_InvalidDelegateType"));
                }

                // We can't deal with secure delegates either.
                if (!InvocationListLogicallyNull() && !_invocationCount.IsNull() && !_methodPtrAux.IsNull())
                {
                    throw new SerializationException(Environment.GetResourceString("Serialization_InvalidDelegateType"));
                }

                DelegateSerializationHolder.GetDelegateSerializationInfo(info, this.GetType(), Target, method, targetIndex);
            }
            else
            {
                DelegateSerializationHolder.DelegateEntry nextDe = null;
                int invocationCount = (int)_invocationCount;
                for (int i = invocationCount; --i >= 0;)
                {
                    MulticastDelegate d      = (MulticastDelegate)invocationList[i];
                    MethodInfo        method = d.Method;
                    // If it is not a RuntimeMethodInfo (must be a DynamicMethod) or if it is an unmanaged function pointer, skip
                    if (!(method is RuntimeMethodInfo) || IsUnmanagedFunctionPtr())
                    {
                        continue;
                    }

                    // We can't deal with secure delegates either.
                    if (!d.InvocationListLogicallyNull() && !d._invocationCount.IsNull() && !d._methodPtrAux.IsNull())
                    {
                        continue;
                    }

                    DelegateSerializationHolder.DelegateEntry de = DelegateSerializationHolder.GetDelegateSerializationInfo(info, d.GetType(), d.Target, method, targetIndex++);
                    if (nextDe != null)
                    {
                        nextDe.Entry = de;
                    }

                    nextDe = de;
                }
                // if nothing was serialized it is a delegate over a DynamicMethod, so just throw
                if (nextDe == null)
                {
                    throw new SerializationException(Environment.GetResourceString("Serialization_InvalidDelegateType"));
                }
            }
        }
All Usage Examples Of System.DelegateSerializationHolder::GetDelegateSerializationInfo