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


C# IFormatter.Serialize方法代码示例

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


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

示例1: SimpleIterativeRouter2

        public SimpleIterativeRouter2(Key self, ushort tcpPort, IMessagingSocket sock, IKeyBasedRoutingAlgorithm algo, IFormatter formatter, bool isStrictMode)
        {
            _selfId = self;
            _tcpPort = tcpPort;
            _sock = sock;
            _algo = algo;
            _strict_mode = isStrictMode;

            // メッセージに含むことの出来る大体の最大NodeHandle数を計算
            int overhead, nodeHandleBytes;
            {
                using (MemoryStream ms = new MemoryStream ()) {
                    formatter.Serialize (ms, new NextHopResponse (self, _tcpPort, true, new NodeHandle[0]));
                    overhead = (int)ms.Length;
                }
                using (MemoryStream ms = new MemoryStream ()) {
                    formatter.Serialize (ms, new NodeHandle (self, new IPEndPoint (IPAddress.Loopback, 0), tcpPort));
                    nodeHandleBytes = (int)ms.Length;
                }
            }
            _maxNodeHandlesPerResponse = (dgramMaxSize - overhead) / nodeHandleBytes;

            algo.Setup (self, this);
            sock.AddInquiredHandler (typeof (NextHopQuery), MessagingSocket_Inquired_NextHopQuery);
            sock.AddInquiredHandler (typeof (CloseNodeQuery), MessagingSocket_Inquired_CloseNodeQuery);
        }
开发者ID:kazuki,项目名称:p2pncs,代码行数:26,代码来源:SimpleIterativeRouter2.cs

示例2: Serialize

 static void Serialize(string filename,IFormatter formatter)
 {
     MyItemType obj = new MyItemType(1, "food");
     FileStream fs = new FileStream(filename,FileMode.OpenOrCreate) ;
     formatter.Serialize(fs, obj);
     fs.Close();
 }
开发者ID:FinleySmile,项目名称:CSharpStudy,代码行数:7,代码来源:Program.cs

示例3: Clone

 /// <summary>
 /// Clone a object with via Serializing/Deserializing
 /// using a implementation of IFormatter. 
 /// <seealso cref="IFormatter"/> 
 /// </summary>
 /// <param name="obj">Object to clone</param>
 /// <param name="formatter">IFormatter implementation in order to Serialize/Deserialize</param>
 /// <returns>Clone object</returns>
 public static object Clone(object obj, IFormatter formatter)
 {
     using (MemoryStream buffer = new MemoryStream())
     {
         formatter.Serialize(buffer, obj);
         buffer.Position = 0;
         return formatter.Deserialize(buffer);
     }
 }
开发者ID:vincentzh,项目名称:nhibernate.burrow-custom,代码行数:17,代码来源:Cloner.cs

示例4: SerializeItem

        public static void SerializeItem( string fileName, IFormatter formatter )
        {
            // Create an instance of the type and serialize it.
            MyItemType t = new MyItemType();
            t.MyProperty = "Hello World";

            System.IO.FileStream s = new System.IO.FileStream( fileName, FileMode.Create );
            formatter.Serialize( s, t );
            s.Close();
        }
开发者ID:notorz,项目名称:useless,代码行数:10,代码来源:Program.cs

示例5: SerializeItem

        public static void SerializeItem(string fileName, IFormatter formatter)
        {
            Author a = new Author();
            a.Name = "Gosho";
            a.Email = "[email protected]";

            FileStream s = new FileStream(fileName, FileMode.Create);
            formatter.Serialize(s, a);
            s.Close();
        }
开发者ID:thedoomx,项目名称:CSharp,代码行数:10,代码来源:Program.cs

示例6: Driver

                void Driver (IFormatter formatter, A a)
                {
                        MemoryStream stream = new MemoryStream();
                        formatter.Serialize(stream, a);
                        stream.Position = 0;

                        a.CheckSerializationStatus ();
                        a = (A) formatter.Deserialize (stream);
                        a.CheckDeserializationStatus ();
                }
开发者ID:nlhepler,项目名称:mono,代码行数:10,代码来源:SerializationCallbackTest.cs

示例7: ProfileSerialization

        private static Stats ProfileSerialization( IFormatter formatter, Stream stream, object objToStore, int numIterations )
        {
            long startTime = TinyTime.CurrentTime;

            for ( int count = 0; count < numIterations; ++count )
            {
                formatter.Serialize( stream, objToStore );
            }

            long endTime = TinyTime.CurrentTime;
            stream.Flush( );

            return new Stats( TinyTime.ToSeconds( startTime, endTime ), stream );
        }
开发者ID:johann-gambolputty,项目名称:robotbastards,代码行数:14,代码来源:TestMuesliPerformance.cs

示例8: Serialize

		public static string Serialize (object o, IFormatter f)
		{
			if (o == null)
				return string.Empty;
			using (MemoryStream ms = new MemoryStream ()) {
				try {
					f.Serialize (ms, o);
				}
				catch (Exception ex) {
					Exception inner = o as Exception;
					if (inner != null)
						RethrowException (inner);
					else
						throw;
				}
				return HttpUtility.UrlEncode (ms.ToArray ());
			}
		}
开发者ID:carrie901,项目名称:mono,代码行数:18,代码来源:MyHost.jvm.cs

示例9: SerializeMovies

 public void SerializeMovies(String filePath)
 {
     formatter = new BinaryFormatter();
     using (fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) {
         formatter.Serialize(fs, movieInfo.Values.ToArray());
         fs.Close();
     }
     //form.DisplayMsg("Saved movies database to " + filePath); (!)
 }
开发者ID:handshou,项目名称:movie-booking,代码行数:9,代码来源:ConnectionHandler.cs

示例10: Serialize

 public void Serialize(String filePath, Dictionary<String, Movie> d)
 {
     formatter = new BinaryFormatter();
     using (fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) {
         formatter.Serialize(fs, d.Values.ToArray());
         fs.Close();
     }
 }
开发者ID:handshou,项目名称:movie-booking,代码行数:8,代码来源:ConnectionHandler.cs

示例11: WriteValueV1

        // WriteValue takes a value and writes it to stream.  It 
        // can take some specific action based on the type, such as write out a compact
        // version of the object if it's a type recognized by this ResourceWriter, or 
        // use Serialization to write out the object using the objFormatter.
        // For instance, the default implementation recognizes primitives such as Int32
        // as special types and calls WriteInt32(int) with the value of the object.  This
        // can be much more compact than serializing the same object.
        // 
        /*
        private void WriteValueV1(Type type, Object value, BinaryWriter writer, IFormatter objFormatter)
        {
            // For efficiency reasons, most of our primitive types will be explicitly
            // recognized here.  Some value classes are also special cased here.
            if (type == typeof(String))
                writer.Write((String) value);
            else if (type == typeof(Int32))
                writer.Write((int)value);
            else if (type == typeof(Byte))
                writer.Write((byte)value);
            else if (type == typeof(SByte))
                writer.Write((sbyte)value);
            else if (type == typeof(Int16))
                writer.Write((short)value);
            else if (type == typeof(Int64))
                writer.Write((long)value);
            else if (type == typeof(UInt16))
                writer.Write((ushort)value);
            else if (type == typeof(UInt32))
                writer.Write((uint)value);
            else if (type == typeof(UInt64))
                writer.Write((ulong)value);
            else if (type == typeof(Single))
                writer.Write((float)value);
            else if (type == typeof(Double))
                writer.Write((double)value);
            else if (type == typeof(DateTime)) {
                // Ideally we should use DateTime's ToBinary & FromBinary,
                // but we can't for compatibility reasons.
                writer.Write(((DateTime)value).Ticks);
            }
            else if (type == typeof(TimeSpan))
                writer.Write(((TimeSpan)value).Ticks);
            else if (type == typeof(Decimal)) {
                int[] bits = Decimal.GetBits((Decimal)value);
                BCLDebug.Assert(bits.Length == 4, "ResourceReader::LoadObject assumes Decimal's GetBits method returns an array of 4 ints");
                for(int i=0; i<bits.Length; i++)
                    writer.Write(bits[i]);
            }
            else if (type == null) {
                BCLDebug.Assert(value == null, "Type is null iff value is null");
#if RESOURCE_FILE_FORMAT_DEBUG
                writer.Write("<null value>");
#endif
            }
            else
                objFormatter.Serialize(writer.BaseStream, value);
        }
        */

        private void WriteValue(ResourceTypeCode typeCode, Object value, BinaryWriter writer, IFormatter objFormatter)
        {
            switch(typeCode) {
            case ResourceTypeCode.Null:
                break;

            case ResourceTypeCode.String:
                writer.Write((String) value);
                break;

            case ResourceTypeCode.Boolean:
                writer.Write((bool) value);
                break;

            case ResourceTypeCode.Char:
                writer.Write((UInt16) (char) value);
                break;

            case ResourceTypeCode.Byte:
                writer.Write((byte) value);
                break;

            case ResourceTypeCode.SByte:
                writer.Write((sbyte) value);
                break;
                
            case ResourceTypeCode.Int16:
                writer.Write((Int16) value);
                break;
                
            case ResourceTypeCode.UInt16:
                writer.Write((UInt16) value);
                break;

            case ResourceTypeCode.Int32:
                writer.Write((Int32) value);
                break;
                
            case ResourceTypeCode.UInt32:
                writer.Write((UInt32) value);
                break;
//.........这里部分代码省略.........
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:101,代码来源:resourcewriter.cs

示例12: Serialize

 /// <summary>
 /// Serialize all the nodes of this tree to the stream provided, using the formatter provided.
 /// </summary>
 /// <param name="stream">The stream to serialize to.</param>
 /// <param name="formatter">The formatter used to serialize.</param>
 public void Serialize(Stream stream, IFormatter formatter)
 {
     formatter.Serialize(stream, this);
 }
开发者ID:Mahdi-K,项目名称:KCore,代码行数:9,代码来源:KTreeView.cs

示例13: WriteValue

    	// WriteValue takes a value and writes it to stream.  It 
    	// can take some specific action based on the type, such as write out a compact
    	// version of the object if it's a type recognized by this ResourceWriter, or 
    	// use Serialization to write out the object using the objFormatter.
    	// For instance, the default implementation recognizes primitives such as Int32
    	// as special types and calls WriteInt32(int) with the value of the object.  This
    	// can be much more compact than serializing the same object.
    	// 
    	private void WriteValue(Type type, Object value, BinaryWriter writer, IFormatter objFormatter)
    	{
    		// For efficiency reasons, most of our primitive types will be explicitly
    		// recognized here.  Some value classes are also special cased here.
    		if (type == typeof(String))
    			writer.Write((String) value);
    		else if (type == typeof(Int32))
    			writer.Write((int)value);
    		else if (type == typeof(Byte))
    			writer.Write((byte)value);
    		else if (type == typeof(SByte))
    			writer.Write((sbyte)value);
    		else if (type == typeof(Int16))
    			writer.Write((short)value);
    		else if (type == typeof(Int64))
    			writer.Write((long)value);
    		else if (type == typeof(UInt16))
    			writer.Write((ushort)value);
    		else if (type == typeof(UInt32))
    			writer.Write((uint)value);
    		else if (type == typeof(UInt64))
    			writer.Write((ulong)value);
    		else if (type == typeof(Single))
    			writer.Write((float)value);
    		else if (type == typeof(Double))
    			writer.Write((double)value);
    		else if (type == typeof(DateTime))
    			writer.Write(((DateTime)value).Ticks);
    		else if (type == typeof(TimeSpan))
    			writer.Write(((TimeSpan)value).Ticks);
    		else if (type == typeof(Decimal)) {
    			int[] bits = Decimal.GetBits((Decimal)value);
                BCLDebug.Assert(bits.Length == 4, "ResourceReader::LoadObject assumes Decimal's GetBits method returns an array of 4 ints");
    			for(int i=0; i<bits.Length; i++)
    				writer.Write(bits[i]);
    		}
    		else if (type == null) {
                BCLDebug.Assert(value == null, "Type is null iff value is null");
#if RESOURCE_FILE_FORMAT_DEBUG
                write.WriteString("<null value>");
#endif
            }
            else
    			objFormatter.Serialize(writer.BaseStream, value);
    	}
开发者ID:ArildF,项目名称:masters,代码行数:53,代码来源:resourcewriter.cs

示例14: WriteExternal

 /// <summary>
 /// The write external.
 /// </summary>
 /// <param name="formatter">
 /// The formatter. 
 /// </param>
 /// <param name="stream">
 /// The stream. 
 /// </param>
 public void WriteExternal(IFormatter formatter, Stream stream)
 {
     formatter.Serialize(stream, this);
 }
开发者ID:alcardac,项目名称:SDMXRI_WS_OF,代码行数:13,代码来源:KeyValueImpl.cs

示例15: remove_instance_from_hash

/*
#if !MONO
  public Object[] remove_instance_from_hash (String instance)
  {
    Object[] ret = new Object[1];

//    assem_perm_list.Remove (instance);
    instances.Remove (instance);

    ret[0]=(Boolean) true;
    return ret;
  }

  public Object[] add_assem_to_sec_hash (String assem_name)
  {
    Object[] ret = new Object[1];

//    assem_perm_list [assem_name] = 1;

    ret[0]=(Boolean) true;
    return ret;
  }
#endif
*/

  #endregion

  #region helper funcs
  private static byte[] obj_serialize_int (Object new_instance, IFormatter bf)
  {
    Object [] ret = new Object[2];

    System.IO.MemoryStream mem_strim = new System.IO.MemoryStream ();
    bf.Serialize(mem_strim, new_instance);
    mem_strim.Flush();

    byte [] byte_array = new byte []{};
    byte_array = mem_strim.ToArray();

    return byte_array;
  }
开发者ID:jplu,项目名称:virtuoso-opensource,代码行数:41,代码来源:virtclr.cs


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