[2.0] One function for saving a class to a file
I would like to have a function could write to a file any class instance that was passed to it.
I read up on seralization, but found that it doesn't work with generics.
Is there any functions/classes in the standard .net library or will I need to write my own stuff.
I'm thinking a recursive function that enumerates fields/methods of a class and records the values for those in an XML-like file.
Re: [2.0] One function for saving a class to a file
Serialisation is the in-built mechanism. If there is no automatic (read: Microsoft already wrote it) serialisation for the object that you want to serailise then you can and will have to write your own serialisation code. An example of custom serialisation is the ReadXml and WriteXml methods of the DataSet. You could use Reflector to look inside those methods and see how Microsoft do it.
Re: [2.0] One function for saving a class to a file
here is an example:
Code:
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace WindowsApplication1
{
[Serializable()] class MyText
{
public MyText()
{
}
public void saveMe(string path)
{
Stream str=File.OpenWrite(path);
BinaryFormatter bf=new BinaryFormatter();
bf.Serialize(str,this);
}
}
}
Re: [2.0] One function for saving a class to a file
Quote:
Originally Posted by SLH
I would like to have a function could write to a file any class instance that was passed to it.
I read up on seralization, but found that it doesn't work with generics.
Is there any functions/classes in the standard .net library or will I need to write my own stuff.
I'm thinking a recursive function that enumerates fields/methods of a class and records the values for those in an XML-like file.
Of course it works with generics. Check your sources mate. I serialize Generics objects all the time and its fine.
Re: [2.0] One function for saving a class to a file
Cool, i'll try that first and see how it goes thanks.
Wish i could remember where i read that because i think it was an MS page!
Re: [2.0] One function for saving a class to a file
Ok, i've got my saved class file:
Code:
<?xml version="1.0"?>
<CEffect_OneShot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GameActions>
<anyType xsi:type="CDrawCards">
<Amount>0</Amount>
</anyType>
</GameActions>
</CEffect_OneShot>
I'm trying to read it in like so: (enumerating all types in my assembly so i can ise one method for all types.
Code:
Type[] Types = System.Reflection.Emit.AssemblyBuilder.GetExecutingAssembly().GetTypes();
List<Type> LTypes = new List<Type>();
foreach (Type type in Types)
{
bool HasConst = false;
ConstructorInfo[] CI = type.GetConstructors();
foreach (ConstructorInfo C in CI)
{
if (C.GetParameters().Length == 0)
HasConst = true;
}
if (!(type.FullName.Contains("Properties.Settings") ||
(HasConst == false) ||
(type.IsSubclassOf(typeof(System.Windows.Forms.Form))) ||
(type.FullName.Contains("RuntimeType"))))
LTypes.Add(type);
}
XmlSerializer Serializer = new XmlSerializer(LTypes[0].GetType(), LTypes.ToArray());
FStream = null;
if (File.Exists("C:\\Temp\\Class.txt"))
{
FStream = File.OpenWrite("C:\\Temp\\Class.txt");
}
else
{
FStream = File.Create("C:\\Temp\\Class.txt");
}
return Serializer.Deserialize(FStream);
Trouble is that i get this error on the new XmlSerializer(....) line:
Code:
System.RuntimeType is inaccessible due to its protection level. Only public types can be processed.
There is no class System.RuntimeType in the list of types or the first type given as parameters, so i'm stumped!
Any explanation as to what's going on would be great - i've tried searching but i've not found anything relevent.
Re: [2.0] One function for saving a class to a file
You're calling .GetType() on LTypes[0], where LTypes is an array of type, which is in essence
Code:
Type t = typeof(something);
t.GetType(); // returns the typeof(t) -- System.RunTimeType
:p
Try taking the .GetType() off of that line.
Re: [2.0] One function for saving a class to a file
Hehe, that was a bit dumb, thanks for pointing it out! :)
I've got a new problem now... i get the following error in the "Serializer.Deserialize(FStream);" line
<CEffect_OneShot xmlns=''> was not expected.
See the save/load code below:
Code:
public void SaveClass(object ClassToSave)
{
Type[] Types = System.Reflection.Emit.AssemblyBuilder.GetExecutingAssembly().GetTypes();
List<Type> LTypes = new List<Type>();
foreach(Type type in Types)
{
bool HasConst = false;
ConstructorInfo[] CI = type.GetConstructors();
foreach (ConstructorInfo C in CI)
{
if (C.GetParameters().Length == 0)
HasConst = true;
}
if(!(type.FullName.Contains("Properties.Settings") || (HasConst == false) || (type.IsSubclassOf(typeof(System.Windows.Forms.Form)))))
LTypes.Add(type);
}
XmlSerializer Serializer = new XmlSerializer(ClassToSave.GetType(),LTypes.ToArray());
FStream = null;
if (File.Exists("C:\\Temp\\Class.txt"))
{
FStream = File.OpenWrite("C:\\Temp\\Class.txt");
}
else
{
FStream = File.Create("C:\\Temp\\Class.txt");
}
Serializer.Serialize(FStream, ClassToSave);
FStream.Close();
}
public object LoadClass(string Path)
{
Type[] Types = System.Reflection.Emit.AssemblyBuilder.GetExecutingAssembly().GetTypes();
List<Type> LTypes = new List<Type>();
foreach (Type type in Types)
{
bool HasConst = false;
ConstructorInfo[] CI = type.GetConstructors();
foreach (ConstructorInfo C in CI)
{
if (C.GetParameters().Length == 0)
HasConst = true;
}
if (!(type.FullName.Contains("Properties.Settings") ||
(HasConst == false) ||
(type.IsSubclassOf(typeof(System.Windows.Forms.Form)))))
LTypes.Add(type);
}
XmlSerializer Serializer = new XmlSerializer(LTypes[0], LTypes.ToArray());
FStream = null;
if (File.Exists("C:\\Temp\\Class.txt"))
{
FStream = File.OpenRead("C:\\Temp\\Class.txt");
}
else
{
FStream = File.Create("C:\\Temp\\Class.txt");
}
return Serializer.Deserialize(FStream);
}
I've not got a clue what's going on here, so any help would be great. :)