Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
.NET Framework GroupsView
.NET Distributed_Apps
.NET
.NET ADO.NET
.NET ASP.NET
.NET ASP.NET Security
.NET ASP.NET Webcontrols
.NET ASP.NET Web Services
.NET Clr
.NET Compact Framework
.NET Drawing
.NET Interop
.NET Performance
.NET Web Services
.NET Windows Forms
.NET Windows Forms Controls
.NET General
.NET Csharp
.NET Visual Basic
.NET Vc
.NET Security
.NET Xml
Vsnet Debugging
Xml
Xsl
Scripting Jscript
Scripting Visual Basicscript
Scripting Wsh
Smartphone Developer
Visual Basic Com
Visual Basic Controls
Visual Basic Crystal
Visual Basic Database Ado
Visual Basic Syntax
Visual Basic Winapi
Vc Atl
Vc Debugger
Vc Language
Vc Mfc
Vc Stl
Visio Developer Visual Basica
Windowsce Embedded Vc
Windows Powershell
Visual Basic Vista Compatibility
Deployment Server
.NET Micro Porting

Group SummariesView
.NET Framework
Access
BizTalk
Certifications
CRM
DDK
Exchange Server
FoxPro
French
French .NET
Games
German
German .NET
Graphic Design
IIS
Internet
ISA Server
Italian
Italian .NET
Maps
MCIS
Miscellaneous
Mobile Apps
Money
MSN
Networking
Office
Ops Mgr
Publisher
Security
SharePoint
Small Business
Spanish
Spanish .NET
SQL Server
Systems Management Server
Transaction Server
Virtual PC / Virtual Server
Visual Studio
Win32
Windows 2000
Windows 2003 Server
Windows 7
Windows Live
Windows Media
Windows Update
Windows Vista
Windows XP
 

View All Microsoft NET Csharp Posts  Ask A New Question 

How can I add generic list to a serializable class?

mark4asp posted on Friday, June 01, 2007 7:55 AM

I need to add an item to this class which I would like to be

public IncumbentManager List<int>;

(alternatively it may be an arraylist, but I prefer a List<int>)

This is the simplifed code below for the Class: ActivityData with just
two variables: ActivityID, ClosingDate.

How do I deal with this in the constructor?

SerializationInfo is a sealed class.

[Serializable()]
private class ActivityData : ISerializable
{
public int ActivityID = 0;
public List<int> IncumbentManager = null;
public DateTime ClosingDate = new DateTime(1753, 1, 1);

public ActivityData()
{
}
protected ActivityData(SerializationInfo info, StreamingContext
context)
{
ActivityID = info.GetInt32("ActivityID");
// What goes here?
// IncumbentManager = info.AddValue("IncumbentManager",
IncumbentManager, List<int>);
ClosingDate = info.GetDateTime("ClosingDate");
}

void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.FullTypeName = "Administration_MandateEdit+ActivityData";
info.AddValue("ActivityID", ActivityID);
// Is this correct?
info.AddValue("IncumbentManager", IncumbentManager);
info.AddValue("ClosingDate", ClosingDate);
}
}

PS: Not all ActivityData objects will have a value for
IncumbentManager.
reply

 

How can I add generic list to a serializable class?

mark4asp posted on Friday, June 01, 2007 8:17 AM

I write this code (below) and I get a compilation error:

System.Collections.Generic.List<int>' is a 'type' but is used like a
'variable'

on this line:       Incumbents = info.GetValue("Incumbents",
List<int>);

yet when I look at SerializationInfo I see this header:

public object GetValue(string name, Type type);



[Serializable()]
private class ActivityData : ISerializable
{
public int ActivityID = 0;
public List<int> Incumbents = null;
public DateTime ClosingDate = new DateTime(1753, 1, 1);

public ActivityData()
{
}
protected ActivityData(SerializationInfo info, StreamingContext
context)
{
ActivityID = info.GetInt32("ActivityID");
Incumbents = info.GetValue("Incumbents", List<int>);
ClosingDate = info.GetDateTime("ClosingDate");
}

void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.FullTypeName = "Administration_MandateEdit+ActivityData";
info.AddValue("ActivityID", ActivityID);
info.AddValue("Incumbents", Incumbents);
info.AddValue("ClosingDate", ClosingDate);
}
}
reply

How can I add generic list to a serializable class?

Jon Skeet [C# MVP] posted on Friday, June 01, 2007 8:31 AM

Looks like you need typeof(List<int>).

I do not know whether that will work at runtime or not, but it will
compile...

Jon
reply

Hi,Unless you class needs to make some custom serialization I would stick with

Ignacio Machin \( .NET/ C# MVP \) posted on Friday, June 01, 2007 9:59 AM

Hi,

Unless you class needs to make some custom serialization I would stick with
the provided mechanism.
List<int> is serializable so unless you have another members in your class
just decorate your class with [Serializable()]

The framework will take care of the case where the member is null for you.
reply

How can I add generic list to a serializable class?

ktrvnbq0 posted on Friday, June 01, 2007 10:21 AM

Additionally you would  need to typecast the return value from the
GetValue(...) call.

i.e. Incumbents = (List<int>) info.GetValue("Incumbents",
typeof(List<int>));
reply


Previous Microsoft NET Csharp conversation.