Example #1
0
        // Constructor.
        public MemberInfoSerializationHolder(SerializationInfo info,
                                             StreamingContext context)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }
            memberType = (MemberTypes)(info.GetInt32("MemberType"));
            name       = info.GetString("Name");
            signature  = info.GetString("Signature");
            String assemblyName = info.GetString("AssemblyName");
            String className    = info.GetString("ClassName");

            if (assemblyName == null || className == null)
            {
                throw new SerializationException
                          (_("Serialize_StateMissing"));
            }
            Assembly assembly = FormatterServices.GetAssemblyByName
                                    (assemblyName);

            if (assembly == null)
            {
                throw new SerializationException
                          (_("Serialize_StateMissing"));
            }
            containingType = FormatterServices.GetTypeFromAssembly
                                 (assembly, className);
        }
Example #2
0
            // Convert a delegate serialization entry into a delegate.
            public Delegate ToDelegate()
            {
                Assembly assem;
                Type     delegateType;
                Type     targetType;

                // Validate the entry state.
                if (methodName == null || methodName.Length == 0)
                {
                    throw new SerializationException
                              (_("Serialize_StateMissing"));
                }

                // Fetch the delegate type.
                assem = FormatterServices.GetAssemblyByName(assembly);
                if (assem == null)
                {
                    throw new SerializationException
                              (_("Serialize_UnknownAssembly"));
                }
                delegateType = assem.GetType(type);
                if (delegateType == null)
                {
                    throw new SerializationException
                              (_("Serialize_StateMissing"));
                }

                // Fetch the target type.
                assem = FormatterServices.GetAssemblyByName
                            (targetTypeAssembly);
                if (assem == null)
                {
                    throw new SerializationException
                              (_("Serialize_UnknownAssembly"));
                }
                targetType = assem.GetType(targetTypeName);
                if (targetType == null)
                {
                    throw new SerializationException
                              (_("Serialize_StateMissing"));
                }

                // Check that the target is an instance of
                // the specified target type.
                if (target != null)
                {
                    if (!targetType.IsInstanceOfType(target))
                    {
                        throw new SerializationException
                                  (_("Serialize_DelegateTargetMismatch"));
                    }
                }

                // Create the Delegate.
                Delegate del;

                if (target != null)
                {
                    del = Delegate.CreateDelegate
                              (delegateType, target, methodName);
                }
                else
                {
                    del = Delegate.CreateDelegate
                              (delegateType, targetType, methodName);
                }

                // Fail if the method is non-public.
                MethodInfo method = del.Method;

                if (method != null && !(method.IsPublic))
                {
                    throw new SerializationException
                              (_("Serialize_DelegateNotPublic"));
                }
                return(del);
            }
Example #3
0
        // Implement the IObjectReference interface.
        public Object GetRealObject(StreamingContext context)
        {
            Assembly assem;

            switch (type)
            {
            case UnityType.Empty:           return(Empty.Value);

            case UnityType.DBNull:          return(DBNull.Value);

            case UnityType.Missing:         return(Missing.Value);

            case UnityType.ClrType:
            {
                if (data == null || data.Length == 0 ||
                    assembly == null)
                {
                    throw new SerializationException
                              (_("Serialize_StateMissing"));
                }
                if (assembly == String.Empty)
                {
                    return(Type.GetType(data));
                }
                assem = FormatterServices.GetAssemblyByName(assembly);
                if (assem == null)
                {
                    throw new SerializationException
                              (_("Serialize_StateMissing"));
                }
                Type clrType =
                    FormatterServices.GetTypeFromAssembly(assem, data);
                if (clrType != null)
                {
                    return(clrType);
                }
                throw new SerializationException
                          (_("Serialize_StateMissing"));
            }
            // Not reached.

            case UnityType.Module:
            {
                assem = FormatterServices.GetAssemblyByName(assembly);
                if (assem == null)
                {
                    throw new SerializationException
                              (_("Serialize_StateMissing"));
                }
                try
                {
                    Module module = assem.GetModule(data);
                    if (module == null)
                    {
                        throw new SerializationException
                                  (_("Serialize_StateMissing"));
                    }
                    return(module);
                }
                catch (Exception)
                {
                    throw new SerializationException
                              (_("Serialize_StateMissing"));
                }
            }
            // Not reached.

            case UnityType.Assembly:
            {
                assem = FormatterServices.GetAssemblyByName(data);
                if (assem == null)
                {
                    throw new SerializationException
                              (_("Serialize_StateMissing"));
                }
                return(assem);
            }
            // Not reached.

            default:
                throw new ArgumentException
                          (_("Arg_InvalidUnityObject"));
            }
        }