Is the following code correct for serializing a quasi-single class, that is
a class that has a descreet (though more than one, so not a true singleton)
number of static instances and no dynamically creatable instances? Please
forgive my use of terms. Quasi-signleton is a term I made up and static
instances is a little bogus too but I don't know the correct terms for what
I mean if there are any. However, I think the code below should be self
explanitory in this regard.
using System;
using System.Runtime.Serialization;
namespace SWIFT.Configuration.Workflow
{
/// <summary>
/// This class represents the various types of object which the swift
system is comprised of.
/// </summary>
/// <remarks>
/// There is only one intance of this class per each represented SWIFT
object type, similar to how there exists only one
/// instance of a System.Type object per class.
/// </remarks>
[Serializable]
public class ObjectType: ISerializable
{
private enum pObjectTypeEnum
{
Workspace,
Document,
Task,
User,
DataForm
}
public static readonly ObjectType Workspace = new
ObjectType(pObjectTypeEnum.Workspace,
"d37bdf57-8298-416e-b880-51892b78f63a", "Workspace");
public static readonly ObjectType Document = new
ObjectType(pObjectTypeEnum.Document, "289ef8ef-f56b-4b01-92b4-0191b222ddc3",
"Document");
public static readonly ObjectType Task = new
ObjectType(pObjectTypeEnum.Task, "e06b7ce4-3d30-45e4-bf94-118046be851f",
"Task");
public static readonly ObjectType User = new
ObjectType(pObjectTypeEnum.User, "17aedaeb-dfcb-4cf3-a13b-d6f02195a81f",
"User");
public static readonly ObjectType DataForm = new
ObjectType(pObjectTypeEnum.DataForm, "ace70065-d136-402f-96f3-0606cded7754",
"Data Form");
private static readonly System.Collections.Hashtable mcTypesByID = new
System.Collections.Hashtable();
public readonly string SysKey;
public readonly string Name;
private readonly int miType;
private ObjectType(pObjectTypeEnum eType, string sKey, string sName)
{
SysKey = sKey;
Name = sName;
miType = (int)eType;
mcTypesByID.Add(miType, this);
}
public new void GetObjectData(SerializationInfo info, StreamingContext
context)
{
info.AddValue("TypeID", miType);
info.SetType(typeof(ObjectType.ObjectTypeSerialize r));
}
internal ObjectType GetByType(int TypeID)
{
return (ObjectType)mcTypesByID[TypeID];
}
[Serializable]
private class ObjectTypeSerializer : IObjectReference, ISerializable
{
private readonly int miType;
private ObjectTypeSerializer(SerializationInfo info, StreamingContext
context)
{
miType = info.GetInt32("TypeID");
}
//required as part of ISerializable
public void GetObjectData(SerializationInfo info, StreamingContext
context) {}
public object GetRealObject(StreamingContext context)
{
return WFDataType.GetByType(miType);
}
}
}
}