1

I previously asked the question How to save/restore serializable object to/from file? that solution works great when I know what type of object I will be saving.

It will be nice if I could save an object of unknown type.

Anyways I am looking to do something like:

[Serializable]
MyCustomClass
{
   // properties. I don't know which properties this class has. I might pass a different class. 
}


public void saveObjectInComputer(object obj, string pathWhereToSaveObject)
{
     // code
}

 public object deserializeObject(string pathWhereObjectIsLocated)
 {
      // code to retrive object
      // ...

      return object;
 }

static void Main(string[] args)
{
     List<MyCustomClass> myList = new List<MyCustomClass>();
     myList.add(new MyCustomClass { "properties go here" } );

     saveObjectInComputer(myList, @"C:\Temp\object.txt");

     // code  

     // Later it will be nice if I could retrive the object 

     object a = deserializeObject(@"C:\Temp\object.txt");

     // then cast a to List<MyCustomClass>

}

And later if I construct a list of cars or a list of people I could use the same method instead of constructing a new method for each. Moreover later I know that the method I am looking for will return an object and I could cast that object to the type that I am currently working with.

Community
  • 1
  • 1
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • you shouldn't edit someone else's answer to include more information about your problem; you should either comment on the answer, or edit your question again to include the additional information. (Also, please break long sections into paragraphs. :) – sarnold May 25 '11 at 03:14

1 Answers1

0

You cannot serialize an object that has not been marked with the Serializable attribute easily, so the short answer is no.

If you're willing to step into moderately difficult code, you could use reflection and devise a custom format to save states with, using exceptions for things like indexed properties, etc.. You might as well just not, though.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • there's a missing "with BinaryFormatter" in here somewhere. There are a myriad of serializers available, and very few care about `[Serializable]`. – Marc Gravell May 25 '11 at 06:13
  • Most serializers are not what I would call complex (from the user-perspective) – Marc Gravell May 25 '11 at 18:25
  • I was more talking about built-in. I personally strongly dislike using external libraries unless absolutely necessary - and in this case it looks like it's not absolutely necessary, it's just for convenience and at the cost of speed, too. – Ry- May 25 '11 at 18:57
  • @minitech - I have no such dislike - and I've written sever libs for thing like this (serialization, materialization (think: DataContext.ExecuteQuery) that **far** out-perform the MS offerings. You don't have to sacrifice performance here. – Marc Gravell May 25 '11 at 21:22
  • What I was talking about there was just saving things manually, which is always faster than reflection, no exceptions. – Ry- May 25 '11 at 23:03
  • @minitech actually, a well written meta-programming serializer (with either pre-generation or cached on-demand IL emit) can be exactly as fast as hand-coded. – Marc Gravell May 25 '11 at 23:09
  • Huh. I've changed my mind, then, I'd like to download one of those some time :D – Ry- May 25 '11 at 23:44
  • 1
    Sure thing - [dapper-dot-net](http://code.google.com/p/dapper-dot-net/) is what we (stackoverflow/stackexchange) use when reading (not editing) data from the DB - faster than L2S (or anything else). [protobuf-net](http://code.google.com/p/protobuf-net/) is AFAIK the fastest serializer for .NET – Marc Gravell May 26 '11 at 05:32
  • Okay, that really helps since I'm in the middle of making a forum-type thing right now!! Too bad that's not an answer... – Ry- May 26 '11 at 18:02