Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

Using generic list with SoapFormatter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hotlips - 12 Dec 2005 11:48 GMT
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?
Hotlips - 12 Dec 2005 18:55 GMT
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);
 }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.