コード例 #1
0
        private static TTransportableObjectType Expand <TTransportableObjectType>(Stream objStream)
            where TTransportableObjectType : TransportableObject
        {
            if (objStream == null)
            {
                throw new ArgumentNullException("objStream", "A valid non-null Stream is required.");
            }

            TTransportableObjectType objTransportableObject = default(TTransportableObjectType);

            using (BinaryReaderExtension objBinaryReader = new BinaryReaderExtension(objStream))
            {
                byte[] bytSignature = objBinaryReader.ReadBytes(RemotableSignature.Length);
                if (AreByteArraysEqual(bytSignature, RemotableSignature) == false)
                {
                    throw new Exception("The binary data does not represent a valid, serialized ITransportableObject instance.");
                }

                string strAssemblyName = objBinaryReader.ReadString();
                string strTypeName     = objBinaryReader.ReadString();

                objTransportableObject = FastReflectionManager.CreateInstance <TTransportableObjectType>(typeof(TTransportableObjectType), new Type[] { typeof(BinaryReaderExtension) }, objBinaryReader);
            }

            return(objTransportableObject);
        }
コード例 #2
0
        private static ITransportableObject Expand(Stream objStream)
        {
            if (objStream == null)
            {
                throw new ArgumentNullException("objStream", "A valid non-null Stream is required.");
            }

            ITransportableObject objTransportableObject = null;

            using (BinaryReaderExtension objBinaryReader = new BinaryReaderExtension(objStream))
            {
                byte[] bytSignature = objBinaryReader.ReadBytes(RemotableSignature.Length);
                if (AreByteArraysEqual(bytSignature, RemotableSignature) == false)
                {
                    throw new Exception("The binary data does not represent a valid, serialized ITransportableObject instance.");
                }

                string strAssemblyName = objBinaryReader.ReadString();
                string strTypeName     = objBinaryReader.ReadString();

                Assembly objAssembly = null;
                try
                {
                    objAssembly = Assembly.Load(strAssemblyName);
                }
                catch (Exception objException)
                {
                    string strErrorMessage = "An error was encountered while loading the assembly - Assembly.Load('" + strAssemblyName + "'):\n";
                    throw new Exception(strErrorMessage, objException);
                }

                Type objType = null;
                try
                {
                    objType = objAssembly.GetType(strTypeName, true, true);
                }
                catch (Exception objException)
                {
                    string strErrorMessage = "An error was encountered while loading the type - objAssemblyName.GetType('" + strTypeName + "', True, True):\n";
                    throw new Exception(strErrorMessage, objException);
                }

                try
                {
                    objTransportableObject = FastReflectionManager.CreateInstance <ITransportableObject>(objType, new Type[] { typeof(BinaryReaderExtension) }, objBinaryReader);
                }
                catch (Exception objException)
                {
                    string strErrorMessage = "An error was encountered while creating the object - Activator.CreateInstance('" + objType.FullName + "'):\n";
                    throw new Exception(strErrorMessage, objException);
                }
            }

            return(objTransportableObject);
        }