.NET Framework - How can I add generic list to a serializable class?

Asked By mark4asp
01-Jun-07 07: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.
SerializationInfo
(1)
StreamingContext
(1)
DateTime
(1)
GetObjectData
(1)
GetDateTime
(1)
AddValue
(1)
GetValue
(1)
GetInt32
(1)
  mark4asp replied...
01-Jun-07 08: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);
}
}
  Jon Skeet [C# MVP] replied...
01-Jun-07 08: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
  Ignacio Machin \( .NET/ C# MVP \) replied...
01-Jun-07 09: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.
  ktrvnbq0 replied...
01-Jun-07 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>));
help
Serializable] public class Content { public string Text { get; set; } public Guid ID { get; set; } public DateTime DateFrom { get; set; } public DateTime DateTo { get; set; } public int Count { get; set; } public DateTime LastCount { get; set; } public int MaxCount { get; set; } public bool IsActive { get; set; } public Content this.ID = id; } } [Serializable] public class ContentContainers : Dictionary<string, ContentContainer> { public ContentContainers() : base() { } public ContentContainers(SerializationInfo info, StreamingContext context) : base(info, context) { } } [. . .] Das LINQ Query so. [. . .] ContentContainer cCont = LoadContentContainer(id); ContentContainer cContainer = (ContentContainer)cCont.Where(c = > (c.IsActive) && (DateTime.Now > = c.DateFrom) && (DateTime.Now < = c.DateTo)).ToList(); [. . .] Die Fehlermeldung: [. . .] Das Objekt des Typs "System.Collections.Generic.List`1 http: / / blog.geniali.ch / http: / / jotd.geniali.ch C# - German Discussions System.Collections.Generic (1) SerializationInfo (1) StreamingContext (1) DateTime (1) GENiALi (1) LINQ (1) Hallo Roland, Roland Schumacher schrieb: Das
DateTime.Compare question .NET Framework string dateTime = "5 / 28 / 2008"; DateTime.Compare(DateTime.Now, DateTime.Now) / / returns 0 DateTime.Compare(DateTime.Now, DateTime.Parse(dateTime)) / / returns 1 I need to compare 2 dates, using the second format, and
Converting DateTime? to itself throws invalidcastexception .NET Framework Convert.ChangeType((DateTime?)DateTime.Now, typeof(DateTime?), null); throws exception Invalid cast from 'System.DateTime' to 'System.Nullable`1[[System.DateTime, mscorlib, Version = 2.0.5.0, Culture = neutral, PublicKeyToken = 7cec85d7bea7798e]]'. How to fix this ? How before calling it or is there better solution ? Andrus. C# Discussions Convert.ChangeType (1) System.DateTime (1) DateTime (1) ChangeType (1) Nullable (1) PublicKeyToken (1) Usenew (1) Beamer (1) Use new Nullable<DateTime
am trying to insert data with the following: mee.Insert(5, currentUser.ProviderUserKey.ToString(), x, "", "", "", DateTime.Now , DateTime.Now , DateTime.Now , DateTime.Now) But get the following error: The best overloaded method match for 'DataSet1TableAdapters.Picks1TableAdapter.Insert(int?, string, string, string, string, string, System.DateTime?, System DateTime?, System.DateTime?, System.DateTime?)' has some invalid arguments All allow nulls. C# Discussions DateTime.NowEven (1) System.DateTime (1