当前位置: 首页>>代码示例>>C#>>正文


C# BinaryFormatter.GetType方法代码示例

本文整理汇总了C#中System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryFormatter.GetType方法的具体用法?C# BinaryFormatter.GetType怎么用?C# BinaryFormatter.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Runtime.Serialization.Formatters.Binary.BinaryFormatter的用法示例。


在下文中一共展示了BinaryFormatter.GetType方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: LoadItems


//.........这里部分代码省略.........

					// ready object
					object o;
				
					// check for compression
					if((flag & F_COMPRESSED) != 0) 
					{
						try 
						{
							// read the input stream, and write to a byte array output stream since
							// we have to read into a byte array, but we don't know how large it
							// will need to be, and we don't want to resize it a bunch
							GZipInputStream gzi = new GZipInputStream(new MemoryStream(buf));
							MemoryStream bos = new MemoryStream(buf.Length);
							
							int count;
							byte[] tmp = new byte[2048];
							while((count = gzi.Read(tmp, 0, tmp.Length)) > 0)
							{
								bos.Write(tmp, 0, count);
							}
							
							// store uncompressed back to buffer
							buf = bos.ToArray();
							gzi.Close();
						}
						catch(IOException e) 
						{
							if(log.IsErrorEnabled)
							{
								log.Error(GetLocalizedString("loaditems uncompression IOException").Replace("$$Key$$", key), e);
							}
							throw new IOException(GetLocalizedString("loaditems uncompression IOException").Replace("$$Key$$", key), e);
						}
					}

					// we can only take out serialized objects
					if((flag & F_SERIALIZED) == 0) 
					{
						if(_primitiveAsString || asString) 
						{
							// pulling out string value
							if(log.IsInfoEnabled)
							{
								log.Info(GetLocalizedString("loaditems retrieve as string"));
							}
							o = Encoding.GetEncoding(_defaultEncoding).GetString(buf);
						}
						else 
						{
							// decoding object
							try 
							{
								o = NativeHandler.Decode(buf);    
							}
							catch(Exception e) 
							{
								if(log.IsErrorEnabled)
								{
									log.Error(GetLocalizedString("loaditems deserialize error").Replace("$$Key$$", key), e);
								}
								throw new IOException(GetLocalizedString("loaditems deserialize error").Replace("$$Key$$", key), e);
							}
						}
					}
					else 
					{
						// deserialize if the data is serialized
						try 
						{
							MemoryStream memStream = new MemoryStream(buf);
							o = new BinaryFormatter().Deserialize(memStream);
							if(log.IsInfoEnabled)
							{
								log.Info(GetLocalizedString("loaditems deserializing").Replace("$$Class$$", o.GetType().Name));
							}
						}
						catch(SerializationException e) 
						{
							if(log.IsErrorEnabled)
							{
								log.Error(GetLocalizedString("loaditems SerializationException").Replace("$$Key$$", key), e);
							}
							throw new IOException(GetLocalizedString("loaditems SerializationException").Replace("$$Key$$", key), e);
						}
					}

					// store the object into the cache
					hm[ key ] =  o ;
				}
				else if(END == line) 
				{
					if(log.IsDebugEnabled)
					{
						log.Debug(GetLocalizedString("loaditems finished"));
					}
					break;
				}
			}
		}
开发者ID:JBTech,项目名称:Dot.Utility,代码行数:101,代码来源:MemCachedClient.cs

示例2: Test

        private static void Test()
        {
            SerializableClass targetObj = new SerializableClass();
            Type t = typeof(SerializableClass);

            string filePath = @"d:\tmp\";

            BinaryFormatter binaryFormatter = new BinaryFormatter();
            DataContractSerializer xmlSerializer = new DataContractSerializer(t);
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(t);

            Stopwatch sw = new Stopwatch();

            // DataContractSerializer
            using (Stream stream = File.OpenWrite(filePath + xmlSerializer.GetType())) {
                sw.Start();
                xmlSerializer.WriteObject(stream, targetObj);
                sw.Stop();

                Console.WriteLine(xmlSerializer.GetType().Name + "\t\tシリアライズ\t" + sw.Elapsed);
                sw.Reset();
            }

            using (Stream stream = File.OpenRead(filePath + xmlSerializer.GetType())) {
                sw.Start();
                xmlSerializer.ReadObject(stream);
                sw.Stop();

                Console.WriteLine(xmlSerializer.GetType().Name + "\t\tデシリアライズ\t" + sw.Elapsed);
                sw.Reset();
            }

            // DataContractJsonSerializer
            using (Stream stream = File.OpenWrite(filePath + jsonSerializer.GetType())) {
                sw.Start();
                jsonSerializer.WriteObject(stream, targetObj);
                sw.Stop();

                Console.WriteLine(jsonSerializer.GetType().Name + "\tシリアライズ\t" + sw.Elapsed);
                sw.Reset();
            }

            using (Stream stream = File.OpenRead(filePath + jsonSerializer.GetType())) {
                sw.Start();
                jsonSerializer.ReadObject(stream);
                sw.Stop();

                Console.WriteLine(jsonSerializer.GetType().Name + "\tデシリアライズ\t" + sw.Elapsed);
                sw.Reset();
            }

            // BinaryFormatter
            using (Stream stream = File.OpenWrite(filePath + binaryFormatter.GetType())) {
                sw.Start();
                binaryFormatter.Serialize(stream, targetObj);
                sw.Stop();

                Console.WriteLine(binaryFormatter.GetType().Name + "\t\t\tシリアライズ\t" + sw.Elapsed);
                sw.Reset();
            }

            using (Stream stream = File.OpenRead(filePath + binaryFormatter.GetType())) {
                sw.Start();
                binaryFormatter.Deserialize(stream);
                sw.Stop();

                Console.WriteLine(binaryFormatter.GetType().Name + "\t\t\tデシリアライズ\t" + sw.Elapsed);
                sw.Reset();
            }
        }
开发者ID:wannabenote,项目名称:CsharpSample,代码行数:70,代码来源:Program.cs

示例3: LoadItems


//.........这里部分代码省略.........

					if(log.IsDebugEnabled)
					{
						log.Debug(GetLocalizedString("loaditems header").Replace("$$Key$$", key).Replace("$$Flags$$", flag.ToString(new NumberFormatInfo())).Replace("$$Length$$", length.ToString(new NumberFormatInfo())));
					}
				
					// read obj into buffer
					byte[] buf =(byte[])resultEntry.Value;
					// ready object
					object o;
				
					// check for compression
					if((flag & F_COMPRESSED) != 0) 
					{
						try 
						{
							// read the input stream, and write to a byte array output stream since
							// we have to read into a byte array, but we don't know how large it
							// will need to be, and we don't want to resize it a bunch
							GZipInputStream gzi = new GZipInputStream(new MemoryStream(buf));
							MemoryStream bos = new MemoryStream(buf.Length);
							
							int count;
							byte[] tmp = new byte[2048];
							while((count = gzi.Read(tmp, 0, tmp.Length)) > 0)
							{
								bos.Write(tmp, 0, count);
							}
							
							// store uncompressed back to buffer
							buf = bos.ToArray();
							gzi.Close();
						}
						catch(IOException e) 
						{
							if(log.IsErrorEnabled)
							{
								log.Error(GetLocalizedString("loaditems uncompression IOException").Replace("$$Key$$", key), e);
							}
							throw new IOException(GetLocalizedString("loaditems uncompression IOException").Replace("$$Key$$", key), e);
						}
					}

					// we can only take out serialized objects
					if((flag & F_SERIALIZED) == 0) 
					{
						if(_primitiveAsString || asString) 
						{
							// pulling out string value
							if(log.IsInfoEnabled)
							{
								log.Info(GetLocalizedString("loaditems retrieve as string"));
							}
							o = Encoding.GetEncoding(_defaultEncoding).GetString(buf);
						}
						else 
						{
							// decoding object
							try 
							{
								o = NativeHandler.Decode(buf);    
							}
							catch(Exception e) 
							{
								if(log.IsErrorEnabled)
								{
									log.Error(GetLocalizedString("loaditems deserialize error").Replace("$$Key$$", key), e);
								}
								throw new IOException(GetLocalizedString("loaditems deserialize error").Replace("$$Key$$", key), e);
							}
						}
					}
					else 
					{
						// deserialize if the data is serialized
						try 
						{
							MemoryStream memStream = new MemoryStream(buf);
							o = new BinaryFormatter().Deserialize(memStream);
							if(log.IsInfoEnabled)
							{
								log.Info(GetLocalizedString("loaditems deserializing").Replace("$$Class$$", o.GetType().Name));
							}
						}
						catch(SerializationException e) 
						{
							if(log.IsErrorEnabled)
							{
								log.Error(GetLocalizedString("loaditems SerializationException").Replace("$$Key$$", key), e);
							}
							throw new IOException(GetLocalizedString("loaditems SerializationException").Replace("$$Key$$", key), e);
						}
					}

					// store the object into the cache
					hm[ key ] =  o ;
				
				
			}
		}
开发者ID:javithalion,项目名称:NCache,代码行数:101,代码来源:MemCachedClient.cs

示例4: LoadItems

        /// <summary> This method loads the data from cache into a Map.
        /// 
        /// Pass a SockIO object which is ready to receive data and a HashMap<br/>
        /// to store the results.
        /// 
        /// </summary>
        /// <param name="sock">socket waiting to pass back data
        /// </param>
        /// <param name="hm">hashmap to store data into
        /// </param>
        /// <throws>  IOException if io exception happens while reading from socket </throws>
        private void LoadItems(SockIOPool.SockIO sock, IDictionary hm)
        {
            while (true)
            {
                string line = sock.ReadLine();

                if(log.IsDebugEnabled) log.Debug("line: " + line);

                if (line.StartsWith(VALUE))
                {
                    string[] info = line.Split(' ');
                    string key = info[1];
                    int flag = int.Parse(info[2]);
                    int length = int.Parse(info[3]);

                    if(log.IsDebugEnabled)
                        log.Debug("key: " + key + "\n" +
                            "flags: " + flag + "\n" +
                            "length: " + length);

                    // read obj into buffer
                    byte[] buf = new byte[length];
                    sock.Read(buf);
                    sock.ClearEOL();

                    // ready object
                    object o;

                    // check for compression
                    if ((flag & F_COMPRESSED) != 0)
                    {
                        try
                        {
                            // read the input stream, and write to a byte array output stream since
                            // we have to read into a byte array, but we don't know how large it
                            // will need to be, and we don't want to resize it a bunch
                            GZipInputStream gzi = new GZipInputStream(new MemoryStream(buf));
                            MemoryStream bos = new MemoryStream(buf.Length);

                            int count;
                            byte[] tmp = new byte[2048];
                            while ((count = gzi.Read(tmp, 0, tmp.Length)) != - 1)
                            {
                                bos.Write(tmp, 0, count);
                            }

                            // store uncompressed back to buffer
                            buf = bos.ToArray();
                            gzi.Close();
                        }
                        catch(IOException ioe)
                        {
                            if(log.IsErrorEnabled) log.Error("IOException thrown while trying to uncompress input stream for key: " + key, ioe);

                            throw new IOException("IOException thrown while trying to uncompress input stream for key: " + key);
                        }
                    }

                    // we can only take out serialized objects
                    if ((flag & F_SERIALIZED) == 0)
                    {
                        if(log.IsInfoEnabled) log.Info("this object is not a serialized object.  Stuffing into a string.");

                        o = new string(UTF8Encoding.UTF8.GetChars(buf));
                    }
                    else
                    {
                        // deserialize if the data is serialized
                        BinaryReader ois = new BinaryReader(new MemoryStream(buf));
                        try
                        {
                            o = new BinaryFormatter().Deserialize(ois.BaseStream);

                            if(log.IsInfoEnabled) log.Info("deserializing " + o.GetType().FullName);
                        }
                        catch(Exception e)
                        {
                            if(log.IsErrorEnabled) log.Error("ClassNotFoundException thrown while trying to deserialize for key: " + key, e);

                            throw new IOException("failed while trying to deserialize for key: " + key);
                        }
                    }

                    // store the object into the cache
                    hm.Add(key, o);
                }
                else if (END.Equals(line))
                {
                    if(log.IsDebugEnabled) log.Debug("finished reading from cache server");
//.........这里部分代码省略.........
开发者ID:jrimmer,项目名称:memcached_dotnet,代码行数:101,代码来源:MemCachedClient.cs


注:本文中的System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.GetType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。