using System;
using System.Collecti ons;
using System.Runtime. Serialization.F ormatters.Binar y;
using System.IO;
namespace ConsoleApplicat ion5
{
[Serializable]
struct name
{
public string forname;
public string familyname;
}
[Serializable]
struct address
{
public string street;
public int number;
}
class clsDataBlock
{
public static Hashtable NameToAddress = new Hashtable ();
[STAThread]
static void Main(string[] args)
{
clsDataBlock db=new clsDataBlock ();
string ans="";
while("x" != ans)
{
System.Console. WriteLine ("1 to enter new");
System.Console. WriteLine ("2 to search");
System.Console. WriteLine ("3 to save (serialize)");
System.Console. WriteLine ("4 to load (deserialize)") ;
System.Console. WriteLine ("5 to show data");
System.Console. WriteLine ("x to quit");
System.Console. WriteLine ("-----------------------");
ans = System.Console. ReadLine ();
switch(ans)
{
case "1":db.AddNew() ;
break;
case "2":db.Search() ;
break;
case "3":db.Save ();
break;
case "4":db.Load ();
break;
case "5":db.Show ();
break;
}
}
}
public void AddNew()
{
name nm;
address ad;
string ans;
System.Console. WriteLine ("enter first name (x to exit):");
ans = System.Console. ReadLine();
nm.forname = ans;
System.Console. WriteLine ("enter last name (x to exit):");
ans = System.Console. ReadLine();
nm.familyname = ans;
System.Console. WriteLine ("enter street name (x to exit):");
ans = System.Console. ReadLine();
ad.street = ans;
System.Console. WriteLine ("enter street number (x to exit):");
ans = System.Console. ReadLine();
ad.number = int.Parse (ans);
NameToAddress.A dd(nm,ad);
}
public void Search()
{
name nm2;
string ans2;
System.Console. WriteLine ("enter first name:");
ans2 = System.Console. ReadLine ();
nm2.forname = ans2;
System.Console. WriteLine ("enter last name:");
ans2 = System.Console. ReadLine ();
nm2.familyname = ans2;
System.Console. WriteLine ("working... ");
object sname = NameToAddress[nm2];
if (sname != null)
{
address ad2 = (address)sname;
System.Console. WriteLine(" address: "+ ad2.street + " " +
ad2.number );
}
else
System.Console. WriteLine ("no name like that...");
}
public void Save()
{
FileStream fs = new FileStream ("Store.dat",Fi leMode.OpenOrCr eate
,FileAccess.Wri te );
try
{
Hashtable a = new Hashtable();
a = NameToAddress;
BinaryFormatter bf=new BinaryFormatter ();
bf.Serialize (fs,a );
}
finally
{
fs.Close ();
}
}
public void Load()
{
FileStream fs = new FileStream ("Store.dat",Fi leMode.Open
,FileAccess.Rea d );
try
{
Hashtable a = new Hashtable();
BinaryFormatter bf=new BinaryFormatter ();
a=(Hashtable)bf .Deserialize (fs);
NameToAddress = a;
}
finally
{
fs.Close ();
}
}
public void Show()
{
name nm3;
address ad3;
foreach (DictionaryEntr y entry in NameToAddress )
{
nm3 = (name)entry.Key ;
ad3 = (address)entry. Value;
Console.WriteLi ne ("name= {0} {1}, address= {2} {3}",
nm3.forname,nm3 .familyname ,ad3.street ,ad3.number);
}
}
}
}
Hope it helps
--
Tamir Khason
You want dot.NET? Just ask:
"Please,
www.dotnet.us "
"francois" <fr******@betti nghouses.com_NO SPAM> wrote in message
news:OE******** ******@TK2MSFTN GP12.phx.gbl...
Hi,
I have a webservice that I am using and I would like it to return an XML
serialized version of an object.
the class of the object is defined serializable as the following:
[Serializable]
public class Event
{
}
Also it contains 5 properties returning simple data type such as int and
string. But one of them is an Hashtable. When I run my project i got a run
time exception telling me that :
System.InvalidO perationExcepti on: There was an error reflecting type
'Bos.BizObj.Eve nt'. ---> System.NotSuppo rtedException: The type
System.Collecti ons.Hashtable is not supported because it implements
IDictionary.
at System.Xml.Seri alization.TypeS cope.GetCollect ionElementType( Type type)
Then my question is:
How can I make the Hashtable serializable to make in turn my Event class
serializable?
Also I am a little bit surprised as in the .Net framework reference it is
specified that the
Hashtable class implement the serializable interface... Then it is
puzzling me even more
Thanks a lot,
Francois