Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Typeof() problem
Message
From
14/12/2005 12:44:07
 
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01077796
Message ID:
01077931
Views:
25
Pete,

Could you post the code where you call this method? I tried it and I am not getting any errors. I called it like this:
object o = MyConfigClass.DeSerializeObject("xyz", typeof(MyOtherClass), false);
~~Bonnie




>Hello Keith, here it is ( the code is an excerpt from his article "Building a better .NET Application Configuration Class" )
>
>
>
>/// <summary>
>		/// Deserializes an object from file and returns a reference.
>		/// </summary>
>		/// <param name="Filename">name of the file to serialize to</param>
>		/// <param name="ObjectType">The Type of the object. Use typeof(yourobject class)</param>
>		/// <param name="BinarySerialization">determines whether we use Xml or Binary serialization</param>
>		/// <returns>Instance of the deserialized object or null. Must be cast to your object type</returns>
>		public static object DeSerializeObject(string Filename,Type ObjectType,bool BinarySerialization)
>		{
>			object Instance = null;
>			
>			if (!BinarySerialization)
>			{
>
>				XmlReader reader = null;
>				XmlSerializer serializer = null;
>				FileStream fs = null;
>				try
>				{
>					// Create an instance of the XmlSerializer specifying type and namespace.
>					serializer = new XmlSerializer(ObjectType);
>
>					// A FileStream is needed to read the XML document.
>					fs = new FileStream(Filename, FileMode.Open);
>					reader = new XmlTextReader(fs);
>				
>					Instance = serializer.Deserialize(reader);
>
>				}
>				catch
>				{
>					return null;
>				}
>				finally
>				{
>					if (fs != null)
>						fs.Close();
>
>					if (reader != null)
>						reader.Close();
>				}
>			}
>			else
>			{
>
>				BinaryFormatter serializer = null;
>				FileStream fs = null;
>
>				try
>				{
>					serializer = new BinaryFormatter();
>					fs = new FileStream(Filename, FileMode.Open);
>					Instance = serializer.Deserialize(fs);
>
>				}
>				catch
>				{
>					return null;
>				}
>				finally
>				{
>					if (fs != null)
>						fs.Close();
>				}
>			}
>
>			return Instance;
>		}
>
>
>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform