Originally posted by: Dhaval00
Surely, there are things that can't be serialized like events and delegates. I was just trying to make you aware of the fact that there are multiple issues that you could [or people behind you] could run into. I was the happiest individual the day I first serialized an object and stored in a DB. Three months later, customized my types, made modifications, and voila... serialization's broke.
Originally posted by: xtknight
Edit: the version int field wouldn't work if it actually throws an exception. But you could probably catch and deal with the exception anyway when a newer version of the file is loaded?
This is not a very good programming approach. It is like saying I'll catch the ThreadAbort exception and do my clean up work in the catch block without acknowledging the concepts of the ThreadAbort exception.
My idea here was to tell the user he is using a version of my program too old to handle the file he is loading. I would probably just terminate the open file operation completely. Are you saying that catching the exception is a bad way to do that? It is possible there's another reason the exception could have been thrown and that I showed the user the message I didn't want to, but then it would be a toss-up what to do at that point anyway (basically an unhandled exception). The way I see it, I can be pretty sure an InvalidCastException is being thrown at that particular point because of one reason and one reason only. As long as .NET has the exception handling framework I may as well utilize it at little performance hit. (I don't even want to get into threading.)
[To me it's akin to using int.TryParse() when unsure and handling things from there. I don't see problems with using the try/catch process.]
Refer to this blog entry with regards to the InvalidCastException I pointed earlier:
http://www.hanselman.com/blog/...verToTypeWhatever.aspx
Suzzane Cook on the CLR team had an extensive blog entry a few years ago on this subject:
For example, path matters when determining whether a type is castable to another type. Even if the assemblies containing the types are identical, if they're loaded from different paths, they're considered different assemblies and therefore their types are different. This is one reason why using contexts other than the Load context is risky. You can get into situations where the same assembly is loaded multiple times in the same appdomain (once in the Load context, once in the LoadFrom context, and even several times in neither context), and their corresponding types won't be castable..
The reason why I keep nagging you is I have had to maintain other people's code who did not implement ISerializable, but simply adorned their types with [Serializable]. I can guarantee you that you'll run into hoards of issues when you deal with serialized objects and are trying to load objects dynamically (vis a vis Reflection).
Just be careful, and do it right the first time
😉.
These look like things to keep in mind for the future, but may not apply to my rather simple program. What I do is I input several text files that belong together, process their contents into a bunch of classes to collect all the data, modify some of the data, then serialize all this out into a file that I can easily import again. At this point it's all in one file and not split into multiple txts anymore, has the modifications, and it's also easier to read in. I have the ability to export to standard formats: (at the least) MS Access Database and SQL.
Can you be more specific on what kinds of problems you ran into when you "maintained" the code? Do you mean that you were trying to load a class from that program into another program and had path problems, or you tried to add a feature to the old program and then realized your old files wouldn't import anymore (versioning)? DId you experiment with ways to work around it or did you decide at that point just to implement ISerializable manually?
I don't want it to be frustrating for others to maintain my code. Since my program has other export functions (output MS Access or SQL), I don't think anyone will have to "late bind" my objects into their programs. At worst they will have to add some features to my program, which as long as I implement the versioning framework should not be a difficulty. Am I wrong in assuming that?
I will take a look at implementing ISerializable myself. There is always that "black box" feeling when things work automatically. I am just wondering if automatically doing it all has innate inabilities or whether or not they can be worked around for Joe Sixpack who is not ready to implement ISerializable himself for the whole project when it could just introduce more problems.
In either case, I'm going to wait for your response before I release my program (that's a while away) because you have been very helpful in raising issues about the automatic method. Thanks. You're not raining on my parade, I just want to be sure of the limitations before I decide to undertake implementing it all manually. I need to decide if that's really worth it or not. I know by now I could have done it already to be sure, but I wouldn't have learned anything.
🙂