Using generic list with SoapFormatter

  • Thread starter Thread starter Hotlips
  • Start date Start date
H

Hotlips

I'm trying to use an SoapFormatter with a generic List (List<int>)
(.NET 2.0)
When I use it I get an exception that states that the collections from
Generic-namespace cannot be used with the SoapFormatter.
Why does the SoapFormatter not support generics?
 
I found an answer how to fix it by doing my own serialization. I have
translated it from C++/CLI (C++.NET) into C# and but only tested with
C++.NET (remember the right namespaces!!!!!)

List<int> l;
for (int x=0; x< 10; x++)
l.Add (x);

Serialize:
Stream s = new FileStream("foo.soap", FileMode.Create,
FileAccess.Write, FileShare.None);
SoapFormatter sf = new SoapFormatter();
sf.Serialize(s, l.ToArray() );
s.Close();

Deserialize:
Stream s = new FileStream("foo.soap", FileMode.Open, FileAccess.Read,
FileShare.None);
SoapFormatter sf = new SoapFormatter();
int[] tmp = (int[])(sf.Deserialize(s));
l = new List<int>((System::Collections::Generic::IEnumerable<int>)
tmp);
s->Close();

A generic List<int> inside an object:
[Serializable]
public class Test: ISerializable
{
public List<int> l_ = new List<int>;
public Test() {
for (int i = 9; i>= 0; --i)
{
l_.Add(i);
}
}

internal Test(SerializationInfo si, StreamingContext context) {
int[] tmp = int[](si.GetValue("l_", typeof(int[]) ));
l_ = new
List<int>((System::Collections::Generic::IEnumerable<int>) tmp);
}
public void GetObjectData( SerializationInfo info, StreamingContext
/*context*/ )
{
//SerializationInfo - essentially a property bag
info.AddValue("l_", l_.ToArray(), typeof(int[]));
Type t = this.GetType();
info.AddValue("TypeObj", t);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top