hBifTs

山自高兮水自深!當塵霧消散,唯事實留傳.荣辱不惊, 看庭前花开花落; 去留随意, 望天上云展云舒.

导航

序列化与HashCode..

Posted on 2004-07-06 21:51  hbiftsaa  阅读(2213)  评论(1编辑  收藏  举报
在前一个文章序列化对象 :)中,我提到了
在这里,我使用了序列化,因为序列化的一个好处就是可以还原原始的HashCode,
即,序列化前的对象的GetHashCode()的返回值和序列化后还原的GetHashCode()的返回值是一样的:)


但是,我今天刚刚试了一下,发现不是这样的..示例代码如下:
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;

namespace TestForMapfile_Write
{
    
/// <summary>
    
/// Summary description for Class1.
    
/// </summary>

    class Class1
    
{
        
/// <summary>
        
/// The main entry point for the application.
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
        
{
            Console.WriteLine(
"please input p / c or x / y: ");
            SharedMemory sm 
= new SharedMemory();
            SharedMemory smClient;
            SharedMemory smHT;
            Client client;
            
object obj;
            
switch( Console.ReadLine()) 
            
{            
                
case "p":
                
case "P":                
                    sm.Create( 
10240,"Tiger");
                    client 
= new Client();
                    Console.WriteLine( client.GetHashCode());
                    client 
= new Client();
                    client.email 
= "tiger@msn.com";
                    client.id 
= 0;
                    client.name 
= "tiger";
                    sm.CopyFrom( Ser(client).ToArray());
                    Console.WriteLine( client.GetHashCode());
                    Console.WriteLine(
"ok");
                    Console.ReadLine();
                    
break;
                
case "c":
                
case "C":
                    sm.Open( 
10240,"Tiger");
                    MemoryStream ms 
= new MemoryStream( sm.CopyTo(0));
                    obj 
= Deser(ms);
                    Console.WriteLine( obj.GetHashCode());
                    Console.WriteLine(
"ok");
                    Console.ReadLine();
                    
break;

                
case "x":
                
case "X":
                    smHT 
= new SharedMemory(10234,"smht");
                    smClient 
= new SharedMemory(10234,"smClient");
                    Hashtable ht 
= new Hashtable();
                    obj 
= new Client(1,"tiger","hh");
                    smClient.CopyFrom( Ser(obj).ToArray());
                    ht.Add( obj,
1);
                    obj 
= new Client( 2,"unrule","unruldem");
                    smClient.CopyFrom( Ser(obj).ToArray());
                    ht.Add( obj,
2);
                    smHT.CopyFrom( Ser(ht).ToArray());
                    Console.WriteLine(
"OK");
                    Console.ReadLine();
                    
break;
                
case "y":
                
case "Y":
                    smHT 
= new SharedMemory();
                    smHT.Open(
10234,"smht");
                    obj 
= Deser( new MemoryStream( smHT.CopyTo(0)));
                    smClient 
= new SharedMemory();
                    smClient.Open( 
10234,"smClient");
                    
object obj1 = Deser( new MemoryStream( smClient.CopyTo(0)));
                    
if( ((Hashtable)obj).ContainsKey( (Client)obj1)){
                        Console.WriteLine(
"win");
                    }

                    
else{
                        Console.WriteLine(
"Fail");
                    }

                    
foreach( DictionaryEntry  i in (Hashtable)obj){
                        Console.WriteLine( ((Client)i.Key).id);
                    }

                    Console.ReadLine();
                    
break;
            }

        }


        
public static MemoryStream Ser(object obj){
            MemoryStream ms 
= new MemoryStream();
            BinaryFormatter formatter 
= new BinaryFormatter();
            formatter.Serialize(ms,obj);
            
return ms;
        }


        
public static object Deser(MemoryStream ms){
            BinaryFormatter formatter 
= new BinaryFormatter();
            
return formatter.Deserialize(ms);
        }

    }


    [Serializable]
    
class Client{
        
public int id;
        
public string name;
        
public string email;

        
public Client(){}
        
public Client(int ID,string Name,string Email){
            id 
= ID;
            name 
= Name;
            email 
= Email;
        }

    }

}


注意:使用的时候加入前面的.NET中使用Mapping File 的API :)中提到了SharedMemory类
运行时分别运行两个,一个输入X,一个输入Y

这里,我把两个Client对象放到一个Hashtable中,再分别把这些个对象序列化,通过MappingFile传给另一个EXE程序.
再反序列化.结果,输出的是Fail,但是Hashtable中确实是有原始的Client对象..

结论:
通过这个例子,我发现,HashCode是和程序运行相关的,是相对于这个程序在当前运行期是唯一的.
所以了,我们如果想要把一个对象做为Hashtable中的Key同时把这个对象和这个Hashtable传给其它的应用程序(比如上面示例中的Client和Hashtalbe).请注意其对象的HashCode.:)

如果一定要传Hashtable过去.那么只要这个Hashtable的Key是.NET内置的数据类型,那照样可以正常使用:)

so,我前面文章中提到的那个结论(最上面有写)是错的.希望大家在使用中注意一下子.Have a good luck~