How can I add generic list to a serializable class? - mark4asp

01-Jun-07 07:55:54
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

01-Jun-07 08:17:23
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]

01-Jun-07 08:31:15
Looks like you need typeof(List<int>).

I don't know whether that will work at runtime or not, but it'll
compile...

Jon
reply
 

How can I add generic list to a serializable class? - Ignacio Machin \( .NET/ C# MVP \)

01-Jun-07 09:59:56
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

01-Jun-07 10:21:56
Additionally you'd need to typecast the return value from the
GetValue(...) call.

i.e. Incumbents = (List<int>) info.GetValue("Incumbents",
typeof(List<int>));
reply
 
Using nAnt to simple copy folders/files ??
promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class