471,350 Members | 1,118 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Using generic list with SoapFormatter

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?

Dec 12 '05 #1
1 7304
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::IEnumerab le<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::IEnumerab le<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);
}
}

Dec 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Wiktor Zychla | last post: by
2 posts views Thread by Jarda | last post: by
reply views Thread by seckley | last post: by
reply views Thread by GrandpaB | last post: by
1 post views Thread by Joachim | last post: by
reply views Thread by crazyone | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.