I am writing a class that both serializes and deserializes an Object. I want to be able to use this class with any object I pass to it.
How do I pass the object to the class? I will need to be able to access the class fields for the instance I am sending it and also I will need to access the definition of the class in order to deserialize text back into an object.
ex.
//mainpage.cs code
private void sendObjectToSerialize()
{
var db = new DatabaseHandler();
var myCar = new Car();
myCar.Make = "Chevy";
string ObjectString = db.SerializeObject(myCar);
}
private void SendStringToBeDeserialized()
{
String myString = "SomeSerializeString" //this string would have been serialized using above Obect myCar
var db = new DatabaseHandler();
var myCar = new Car();
var myCar = db.DeserializeStringToObject(String,myCar);
}
//Database Handler Code
public string SerializeObject(Object myObject)
{
var myNewObject = new myObject();//doesn't work - error myObject used as a variable not a type
//...serialize the object passed to this function
}
public Object DeserializeStringToObject(String ObjectString ,Object myObject)
{
var myNewObject = new myObject;//error myObject used as a variable not a type
//convert string to Object of type myObject...
return mynewObject;
}
var myNewObject = new myObject;shouldn't that bevar myNewObject = new myObject();also if you are passing in the object and you havevar my NewObjectwhy don't you just assign it to the param that's being passed in ..? also when show code please show all relevant code.. for example the class definition ofCaryou may want to look up how to useActivator.CreateInstance