views:

427

answers:

2

Suppose you have the following class:

class Test : ISerializable {

  public static Test Instance1 = new Test {
    Value1 = "Hello"
    ,Value2 = 86
  };
  public static Test Instance2 = new Test {
    Value1 = "World"
    ,Value2 = 26
  };

  public String Value1 { get; private set; }
  public int Value2 { get; private set; }

  public void GetObjectData(SerializationInfo info, StreamingContext context) {
    //Serialize an indicator of which instance we are - Currently 
    //I am using the FieldInfo for the static reference.
  }
}

I was wondering if it is possible / elegant to deserialize to the static instances of the class?

Since the deserialization routines (I'm using BinaryFormatter, though I'd imagine others would be similar) look for a constructor with the same argument list as GetObjectData(), it seems like this can't be done directly . . Which I would presume means that the most elegant solution would be to actually use an enum, and then provide some sort of translation mechanism for turning an enum value into an instance reference. However, I personally like that the "Enum"'s choices are directly linked with their data.

How might one go about this?

A: 

Use Enum.Parse...Suppose you have the following:

Enum myEnum{
   Foo = 1,
   Bar = 2,
   Baz = 3
};

Then

   myEnum myE = myEnum.Foo; /* Default! */
   myE = (myEnum)Enum.Parse(myE.GetType(), "Baz"); 
   /* Now, myE should be Baz! */
   Console.WriteLine("Enum Selected: {0}", myE.ToString());

The above sample serves to illustrate how to convert a string literal into an enum. I hope this is what you are looking for.

tommieb75
That is a handy function, but unless you can associate more data than a name / index within the default Enum type, it's not exactly what I'm looking for.
Walt W
+3  A: 

If you need more data with with the Enums, consider using attributes. Example below.

class Name : Attribute
{
    public string Text;

    public Name(string text)
    {
        this.Text = text;
    }
}


class Description : Attribute
{
    public string Text;

    public Description(string text)
    {
        this.Text = text;
    }
}
public enum DaysOfWeek
{
    [Name("FirstDayOfWeek")]
    [Description("This is the first day of 7 days")]
    Sunday = 1,

    [Name("SecondDayOfWeek")]
    [Description("This is the second day of 7 days")]
    Monday= 2,

    [Name("FirstDayOfWeek")]
    [Description("This is the Third day of 7 days")]
    Tuesday= 3,
}

Perhaps this will allow you to provide more information with the Enums. You can access the attributes through reflection. If you need an example to retrieve the attribute I can provide that as well but I'm trying to keep this somewhat short.

galford13x
Well, this works, but it does require a lot of reflection and is not exactly what I was looking for. But with a basic wrapper class, I do suppose it would be very close... the main thing that would be awkward is the name difference between the wrapper class and the enum.
Walt W
I see what you mean. I normally resolve this by by naming the Enum and appending Enum to the end.Enum = DaysOfWeekEnumClass Wrapper = DaysOfWeek
galford13x
Another very convenient way to access the attribute method is through extensions. If your interested in how to do that ask another question regarding extensions.
galford13x