|
-
Jun 8th, 2006, 02:32 AM
#1
Thread Starter
Not NoteMe
[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.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Jun 8th, 2006, 02:48 AM
#2
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.
-
Jun 8th, 2006, 04:16 AM
#3
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);
}
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 8th, 2006, 06:32 AM
#4
Re: [2.0] One function for saving a class to a file
 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.
I don't live here any more.
-
Jun 8th, 2006, 09:14 AM
#5
Thread Starter
Not NoteMe
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!
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Jun 8th, 2006, 10:24 AM
#6
Thread Starter
Not NoteMe
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.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Jun 8th, 2006, 03:15 PM
#7
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
Try taking the .GetType() off of that line.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jun 8th, 2006, 04:46 PM
#8
Thread Starter
Not NoteMe
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.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|