How can deserialize one class to another class like this:
var ser = SerializedObject(b);// read from Database!
var des = DeSerializeAnObject(ser, typeof(BaseClass));
BaseClass baseclass = (BaseClass)des;
baseclass.Hello();
Are you sure it's really serialization/deserialization you're after? Sounds to me like what you're looking for is code to map (ie copy selected or all properties) one object to another.
Have a look at the AutoMapper Getting Started Guide .
In following with your example you could do something like this
BaseClass baseClass = Mapper.Map<OtherClass, BaseClass>(b);
I'm also somewhat confused with your choice of class names in your example. If "BaseClass" really is a base class of OtherClass then you'd just do a cast instead but I'm going to guess that's not the case.
a deserialize sample is like ...
public BaseClass DeSerializeAnObject(BaseClass bc)
{
if (bc == null) return bc;
IFormatter formatter = new BinaryFormatter();
using (Stream stream = new MemoryStream())
{
formatter.Serialize(stream, bc);
stream.Seek(0, SeekOrigin.Begin);
return (BaseClass)formatter.Deserialize(stream);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.