I am trying to get protobuf serialization working with the RedisSessionStateProvider. I have specified the redisSerializerType as a custom class which implements Microsoft.Web.Redis.ISerializer - here is the deserialization code:
public object Deserialize(byte[] data)
{
return DeserializeDirect(data);
}
private object DeserializeDirect(byte[] data)
{
using (var memoryStream = new MemoryStream(data))
{
return Serializer.Deserialize<object>(memoryStream);
}
return null;
}
As I need to implement Microsoft.Web.Redis.ISerializer the signature for deserialize uses a return type of object and there is no way to pass in the actual type being returned. So when DeserializeDirect tries to use the Protobuf.Serializer to deserialize it (as expected) says "Type is not expected, and no contract can be inferred: System.Object". I am using a web app with .NET framework 4.6.1 and I was hoping somebody could point out what I am doing wrong.
Thanks!
Normally, protobuf-net really wants to know the exact type. You can, however, cheat using DynamicType. This tells protobuf-net to include additional type metadata - something it doesn't usually include.
Note that this can make you code brittle - it may fail if the type changes in you code!
I will be implementing Any soon (as part of 2.3.0), which is another option here.
public static void Main()
{
// the actual object we care about
object obj = new Foo { X = 1 };
// serialize and deserialize via stub
var stub = new Stub { Data = obj };
var clone = Serializer.DeepClone(stub);
// prove it worked
Console.WriteLine(clone.Data);
// prove it is a different instance
Console.WriteLine(ReferenceEquals(obj, clone.Data));
}
[ProtoContract]
public class Foo
{
[ProtoMember(1)]
public int X { get; set; }
public override string ToString() => $"X={X}";
}
[ProtoContract]
public sealed class Stub
{
[ProtoMember(1, DynamicType = true)]
public object Data { get; set; }
}
Related
I would like to XML serialize an object that has (among other) a property of type IModelObject (which is an interface).
public class Example
{
public IModelObject Model { get; set; }
}
When I try to serialize an object of this class, I receive the following error:
"Cannot serialize member Example.Model of type Example because it is an interface."
I understand that the problem is that an interface cannot be serialized. However, the concrete Model object type is unknown until runtime.
Replacing the IModelObject interface with an abstract or concrete type and use inheritance with XMLInclude is possible, but seems like an ugly workaround.
Any suggestions?
This is simply an inherent limitation of declarative serialization where type information is not embedded within the output.
On trying to convert <Flibble Foo="10" /> back into
public class Flibble { public object Foo { get; set; } }
How does the serializer know whether it should be an int, a string, a double (or something else)...
To make this work you have several options but if you truly don't know till runtime the easiest way to do this is likely to be using the XmlAttributeOverrides.
Sadly this will only work with base classes, not interfaces. The best you can do there is to ignore the property which isn't sufficient for your needs.
If you really must stay with interfaces you have three real options:
Hide it and deal with it in another property
Ugly, unpleasant boiler plate and much repetition but most consumers of the class will not have to deal with the problem:
[XmlIgnore()]
public object Foo { get; set; }
[XmlElement("Foo")]
[EditorVisibile(EditorVisibility.Advanced)]
public string FooSerialized
{
get { /* code here to convert any type in Foo to string */ }
set { /* code to parse out serialized value and make Foo an instance of the proper type*/ }
}
This is likely to become a maintenance nightmare...
Implement IXmlSerializable
Similar to the first option in that you take full control of things but
Pros
You don't have nasty 'fake' properties hanging around.
you can interact directly with the xml structure adding flexibility/versioning
Cons
you may end up having to re-implement the wheel for all the other properties on the class
Issues of duplication of effort are similar to the first.
Modify your property to use a wrapping type
public sealed class XmlAnything<T> : IXmlSerializable
{
public XmlAnything() {}
public XmlAnything(T t) { this.Value = t;}
public T Value {get; set;}
public void WriteXml (XmlWriter writer)
{
if (Value == null)
{
writer.WriteAttributeString("type", "null");
return;
}
Type type = this.Value.GetType();
XmlSerializer serializer = new XmlSerializer(type);
writer.WriteAttributeString("type", type.AssemblyQualifiedName);
serializer.Serialize(writer, this.Value);
}
public void ReadXml(XmlReader reader)
{
if(!reader.HasAttributes)
throw new FormatException("expected a type attribute!");
string type = reader.GetAttribute("type");
reader.Read(); // consume the value
if (type == "null")
return;// leave T at default value
XmlSerializer serializer = new XmlSerializer(Type.GetType(type));
this.Value = (T)serializer.Deserialize(reader);
reader.ReadEndElement();
}
public XmlSchema GetSchema() { return(null); }
}
Using this would involve something like (in project P):
public namespace P
{
public interface IFoo {}
public class RealFoo : IFoo { public int X; }
public class OtherFoo : IFoo { public double X; }
public class Flibble
{
public XmlAnything<IFoo> Foo;
}
public static void Main(string[] args)
{
var x = new Flibble();
x.Foo = new XmlAnything<IFoo>(new RealFoo());
var s = new XmlSerializer(typeof(Flibble));
var sw = new StringWriter();
s.Serialize(sw, x);
Console.WriteLine(sw);
}
}
which gives you:
<?xml version="1.0" encoding="utf-16"?>
<MainClass
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Foo type="P.RealFoo, P, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<RealFoo>
<X>0</X>
</RealFoo>
</Foo>
</MainClass>
This is obviously more cumbersome for users of the class though avoids much boiler plate.
A happy medium may be merging the XmlAnything idea into the 'backing' property of the first technique. In this way most of the grunt work is done for you but consumers of the class suffer no impact beyond confusion with introspection.
The solution to this is using reflection with the DataContractSerializer. You don't even have to mark your class with [DataContract] or [DataMember]. It will serialize any object, regardless of whether it has interface type properties (including dictionaries) into xml. Here is a simple extension method that will serialize any object into XML even if it has interfaces (note you could tweak this to run recursively as well).
public static XElement ToXML(this object o)
{
Type t = o.GetType();
Type[] extraTypes = t.GetProperties()
.Where(p => p.PropertyType.IsInterface)
.Select(p => p.GetValue(o, null).GetType())
.ToArray();
DataContractSerializer serializer = new DataContractSerializer(t, extraTypes);
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
serializer.WriteObject(xw, o);
return XElement.Parse(sw.ToString());
}
what the LINQ expression does is it enumerates each property,
returns each property that is an interface,
gets the value of that property (the underlying object),
gets the type of that concrete object
puts it into an array, and adds that to the serializer's list of known types.
Now the serializer knows how about the types it is serializing so it can do its job.
If you know your interface implementors up-front there's a fairly simple hack you can use to get your interface type to serialize without writing any parsing code:
public interface IInterface {}
public class KnownImplementor01 : IInterface {}
public class KnownImplementor02 : IInterface {}
public class KnownImplementor03 : IInterface {}
public class ToSerialize {
[XmlIgnore]
public IInterface InterfaceProperty { get; set; }
[XmlArray("interface")]
[XmlArrayItem("ofTypeKnownImplementor01", typeof(KnownImplementor01))]
[XmlArrayItem("ofTypeKnownImplementor02", typeof(KnownImplementor02))]
[XmlArrayItem("ofTypeKnownImplementor03", typeof(KnownImplementor03))]
public object[] InterfacePropertySerialization {
get { return new[] { InterfaceProperty }; ; }
set { InterfaceProperty = (IInterface)value.Single(); }
}
}
The resulting xml should look something along the lines of
<interface><ofTypeKnownImplementor01><!-- etc... -->
You can use ExtendedXmlSerializer. This serializer support serialization of interface property without any tricks.
var serializer = new ConfigurationContainer().UseOptimizedNamespaces().Create();
var obj = new Example
{
Model = new Model { Name = "name" }
};
var xml = serializer.Serialize(obj);
Your xml will look like:
<?xml version="1.0" encoding="utf-8"?>
<Example xmlns:exs="https://extendedxmlserializer.github.io/v2" xmlns="clr-namespace:ExtendedXmlSerializer.Samples.Simple;assembly=ExtendedXmlSerializer.Samples">
<Model exs:type="Model">
<Name>name</Name>
</Model>
</Example>
ExtendedXmlSerializer support .net 4.5 and .net Core.
Replacing the IModelObject interface with an abstract or concrete type and use inheritance with XMLInclude is possible, but seems like an ugly workaround.
If it is possible to use an abstract base I would recommend that route. It will still be cleaner than using hand-rolled serialization. The only trouble I see with the abstract base is that your still going to need the concrete type? At least that is how I've used it in the past, something like:
public abstract class IHaveSomething
{
public abstract string Something { get; set; }
}
public class MySomething : IHaveSomething
{
string _sometext;
public override string Something
{ get { return _sometext; } set { _sometext = value; } }
}
[XmlRoot("abc")]
public class seriaized
{
[XmlElement("item", typeof(MySomething))]
public IHaveSomething data;
}
Unfortunately there's no simple answer, as the serializer doesn't know what to serialize for an interface. I found a more complete explaination on how to workaround this on MSDN
Unfortuantely for me, I had a case where the class to be serialized had properties that had interfaces as properties as well, so I needed to recursively process each property. Also, some of the interface properties were marked as [XmlIgnore], so I wanted to skip over those. I took ideas that I found on this thread and added some things to it to make it recursive. Only the deserialization code is shown here:
void main()
{
var serializer = GetDataContractSerializer<MyObjectWithCascadingInterfaces>();
using (FileStream stream = new FileStream(xmlPath, FileMode.Open))
{
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());
var obj = (MyObjectWithCascadingInterfaces)serializer.ReadObject(reader);
// your code here
}
}
DataContractSerializer GetDataContractSerializer<T>() where T : new()
{
Type[] types = GetTypesForInterfaces<T>();
// Filter out duplicates
Type[] result = types.ToList().Distinct().ToList().ToArray();
var obj = new T();
return new DataContractSerializer(obj.GetType(), types);
}
Type[] GetTypesForInterfaces<T>() where T : new()
{
return GetTypesForInterfaces(typeof(T));
}
Type[] GetTypesForInterfaces(Type T)
{
Type[] result = new Type[0];
var obj = Activator.CreateInstance(T);
// get the type for all interface properties that are not marked as "XmlIgnore"
Type[] types = T.GetProperties()
.Where(p => p.PropertyType.IsInterface &&
!p.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false).Any())
.Select(p => p.GetValue(obj, null).GetType())
.ToArray();
result = result.ToList().Concat(types.ToList()).ToArray();
// do the same for each of the types identified
foreach (Type t in types)
{
Type[] embeddedTypes = GetTypesForInterfaces(t);
result = result.ToList().Concat(embeddedTypes.ToList()).ToArray();
}
return result;
}
I have found a simpler solution (you don't need the DataContractSerializer), thanks to this blog here:
XML serializing derived types when base type is in another namespace or DLL
But 2 problems can rise in this implementation:
(1) What if DerivedBase is not in the namespace of class Base, or even worse in a project that depends on Base namespace, so Base cannot XMLInclude DerivedBase
(2) What if we only have class Base as a dll ,so again Base cannot XMLInclude DerivedBase
Till now, ...
So the solution to the 2 problems is by using XmlSerializer Constructor (Type, array[]) :
XmlSerializer ser = new XmlSerializer(typeof(A), new Type[]{ typeof(DerivedBase)});
A detailed example is provided here on MSDN:
XmlSerializer Constructor (Type, extraTypesArray[])
It seems to me that for DataContracts or Soap XMLs, you need to check the XmlRoot as mentioned here in this SO question.
A similar answer is here on SO but it isn't marked as one, as it not the OP seems to have considered it already.
in my project, I have a
List<IFormatStyle> FormatStyleTemplates;
containing different Types.
I then use the solution 'XmlAnything' from above, to serialize this list of different types.
The generated xml is beautiful.
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[XmlArray("FormatStyleTemplates")]
[XmlArrayItem("FormatStyle")]
public XmlAnything<IFormatStyle>[] FormatStyleTemplatesXML
{
get
{
return FormatStyleTemplates.Select(t => new XmlAnything<IFormatStyle>(t)).ToArray();
}
set
{
// read the values back into some new object or whatever
m_FormatStyleTemplates = new FormatStyleProvider(null, true);
value.ForEach(t => m_FormatStyleTemplates.Add(t.Value));
}
}
I get an object back from a method. The object is of type object and I can't change this because of backwards compatibility. If it is of a certain type (Response<T> bellow) then I need to access the property Payload which is of type T so that I can serialize it as part of a another object and send it off. The problem is that since I don't know the type of T I can't cast the object to Response<T> to access Payload even though I don't care about its type.
Here is my object with the generic type:
public class Response
{
public int Status { get; set; }
public string Message { get; set; }
}
public class Response<T> : Response
{
public T Payload { get; set; }
}
Here is what I would like to do:
// Will sometimes be of type Response<T>
object data = LegacyCode();
if (data.GetType().IsGenericType && data.GetType().GetGenericTypeDefinition() == typeof(Response<>)) {
var payload = ((Response<object>)data).Payload; // Unable to cast object of type...
}
But the only way I could find to do this is by using dynamics.
// Will sometimes be of type Response<T>
object data = LegacyCode();
if (data.GetType().IsGenericType && data.GetType().GetGenericTypeDefinition() == typeof(Response<>)) {
var payload = ((dynamice)data).Payload;
}
Don't ask why things are the way they are (I'm wondering that myself). I have to do code gymnastics to keep backwards compatibility in this system. I just want compile time checking on the name of the property.
Here is a fiddle: https://dotnetfiddle.net/dXxHbD
UPDATE:
I need to be able to serialize and deserialize this object. Originally Response had a property Payload of type object. This caused serialization issues when Response<T> was deserialized because the Payload property was of type Newtonsoft.Json.Linq.JObject which could not be cast to T. Here is an example: https://dotnetfiddle.net/uc15HD
The problem was that I was going the wrong direction and the deserialization works if I cast T to object rather than try to cast object to T. When I store the value as its specific type T then the serializer knows what to deserialize the string to.
Here is an example using Jon's answer below: https://dotnetfiddle.net/KwudAx
Here is a similar example using Matias' solution of using covariance: https://dotnetfiddle.net/kCjZr4
To get compile-time checking of the name of the property, you can keep the dynamic typing, but get the runtime "mini-compiler" to do the hard work:
object data = LegacyCode();
object payload = GetPayload(data);
// Use payload
...
private static object GetPayload<T>(Response<T> response)
{
return response.Payload;
}
public static object GetPayload(object data)
{
// Fallback method. You could return null here and use
// that to indicate in the calling code that it wasn't a
// Response<T>...
}
A much better solution would be to add an non-generic interface or an extra base class though. For example:
public class Response
{
public int Status { get; set; }
public string Message { get; set; }
}
public interface IPayloadHolder
{
public object Payload { get; }
}
public class Response<T> : Response, IPayloadHolder
{
public T Payload { get; set; }
// By using explicit interface implementation, this
// doesn't get in the way for normal usage.
IPayloadHolder.Payload { get { return Payload; } }
}
Then you can use:
var payloadHolder = data as IPayloadHolder;
if (payloadHolder != null)
{
var payload = payloadHolder.Payload;
}
I believe that you need to use covariance.
Design an interface IResponse<out T>:
public interface IResponse<out T>
{
public T Payload { get; }
}
And implement it on Response<T>. Now you can cast it to IResponse<object>:
Response<string> x = new Response<string>();
IResponse<object> y = x; // implicit cast
I'm using protobuf-net v2 and have a class that inherits "List" that I wan't to serialize/clone.
when I'm calling "DeepClone" (or deserialize) I'm getting the cloned object empty.
I can serialize the object into file and it seems to be serialized as expected but the RuntimeTypeModel can't deserialized it back from the byte[].
the single solution I've found to overcome this issue is to use surrogate.
as said, if you're skipping the "SetSurrogate" the clone is failed.
is there any other option to solve it?
attached:
class Program
{
static void Main(string[] args)
{
RuntimeTypeModel model = RuntimeTypeModel.Create();
model[typeof(Custom<string>)].SetSurrogate(typeof(Surrogate<string>));
var original = new Custom<string> { "C#" };
var clone = (Custom<string>)model.DeepClone(original);
Debug.Assert(clone.Count == original.Count);
}
}
[ProtoContract(IgnoreListHandling = true)]
public class Custom<T> : List<T> { }
[ProtoContract]
class Surrogate<T>
{
public static implicit operator Custom<T>(Surrogate<T> surrogate)
{
Custom<T> original = new Custom<T>();
original.AddRange(surrogate.Pieces);
return original;
}
public static implicit operator Surrogate<T>(Custom<T> original)
{
return original == null ? null : new Surrogate<T> { Pieces = original };
}
[ProtoMember(1)]
internal List<T> Pieces { get; set; }
}
Another thing I've found is when you're replacing the ProtoContract attribute from the class "Custom" with "System.Serializable" attribute, it deserializing the byte[] as expected even without surrogate.
The problem here is simply the fact that you explicitly turned off the list-handling, via:
[ProtoContract(IgnoreListHandling = true)]
public class Custom<T> : List<T> { }
Which, as the name suggests and the documentation verifies:
/// <summary>
/// If specified, do NOT treat this type as a list, even if it looks like one.
/// </summary>
public bool IgnoreListHandling {...}
So: there was nothing useful left to do, as Custom<T> doesn't have any other data-members to serialize.
So: if you aren't using the surrogate, don't disable list-handling. The main purpose of this option is for edge-cases where something that is intended to be an "object" also has features that make it look temptingly like a list (all protobuf-net needs is IEnumerable[<T>] and a handy Add(T) method).
[TestFixture]
public class SO11034791
{
[Test]
public void Execute()
{
RuntimeTypeModel model = RuntimeTypeModel.Create();
var original = new Custom<string> { "C#" };
var clone = (Custom<string>)model.DeepClone(original);
Assert.AreEqual(1, clone.Count);
Assert.AreEqual("C#", clone.Single());
}
public class Custom<T> : List<T> { }
}
I've got a class which has been serialized into JSON, and which I'm trying to deserialize into an object.
e.g.
public class ContentItemViewModel
{
public string CssClass { get; set; }
public MyCustomClass PropertyB { get; set; }
}
the simple property (CssClass) will deserialize with:
var contentItemViewModels = ser.Deserialize<ContentItemViewModel>(contentItems);
But PropertyB gets an error...
We added a JavaScriptConverter:
ser.RegisterConverters(new List<JavaScriptConverter>{ publishedStatusResolver});
But when we added 'MyCustomClass' as a 'SupportedType', the Deserialize method was never called. However when we have ContentItemViewModel as the SupportedType, then Deserialize is called.
We've got a current solution which looks something like this:
class ContentItemViewModelConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
var cssClass = GetString(dictionary, "cssClass"); //I'm ommitting the GetString method in this example...
var propertyB= GetString(dictionary, "propertyB");
return new ContentItemViewModel{ CssClass = cssClass ,
PropertyB = new MyCustomClass(propertyB)}
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
throw new Exception("Only does the Deserialize");
}
public override IEnumerable<Type> SupportedTypes
{
get
{
return new List<Type>
{
typeof(ContentItemViewModel)
};
}
}
}
But we'd prefer a simpler solution of only deserializing MyCustomClass, as there are a number of other fields which are on the ViewModel, and it seems a waste to have to edit this converter every time we change/add a property....
Is there a way to Deserialize JUST PropertyB of type MyCustomClass?
Thanks for your help!
Have you considered using DatacontractJsonSerializer
[DataContract]
public class MyCustomClass
{
[DataMember]
public string foobar { get; set; }
}
[DataContract]
public class ContentItemViewModel
{
[DataMember]
public string CssClass { get; set; }
[DataMember]
public MyCustomClass PropertyB { get; set; }
}
class Program
{
static void Main(string[] args)
{
ContentItemViewModel model = new ContentItemViewModel();
model.CssClass = "StackOver";
model.PropertyB = new MyCustomClass();
model.PropertyB.foobar = "Flow";
//Create a stream to serialize the object to.
MemoryStream ms = new MemoryStream();
// Serializer the User object to the stream.
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ContentItemViewModel));
ser.WriteObject(ms, model);
byte[] json = ms.ToArray();
ms.Close();
string s= Encoding.UTF8.GetString(json, 0, json.Length);
Console.ReadLine();
}
}
Add all possible classes to DatacontractJsonSerializer.KnownTypes if MyCustomClass has derivations.
For whatever it may be worth after all this time, but I stumbled over the same problem and the solution is that the Deserializer hasn't got a clue about the classes you are deserializing unless you give him the necessary information.
On the top level, it knows the type from the type parameter of Deserialize<>(). That's why your converter for ContentItemViewModel works. For nested objects, it needs __type properties and a JavaScriptTypeResolver.
var ser = new JavaScriptSerializer(new SimpleTypeResolver());
ser.RegisterConverters(myconverters);
MyClass myObject = new MyClass();
string json = ser.Serialize(myObject);
// set a breakpoint here to see what has happened
ser.Deserialize<MyClass>(json);
A TypeResolver adds a __type property to each serialized object. You can write a custom type resolver that uses short names. In this sample, I use the SimpleTypeResolver from .net that "simply" stores the fully qualified type name as __type. When deserializing, the JavaScriptDeserializer finds __type and asks the TypeResolver for the correct type. Then it knows a type and can call a registered JavaScriptConverter.Deserialize method.
Without a TypeResolver, objects are deserialized to a Dictionary because JavaScriptSerializer doesn't have any type information.
If you can't provide a __type property in your json string, I think you'll need to deserialize to Dictionary first and then add a "guessing-step" that interprets the fields to find the right type. Then, you can use the ConvertToType method of JavaScriptSerializer to copy the dictionary into the object's fields and properties.
If you need to use the JavaScriptSerializer that is provides by ASP.NET and can't create your own, consider this section from the .ctor help of JavaScriptSerializer:
The instance of JavaScriptSerializer that is used by the asynchronous communication layer for invoking Web services from client script uses a special type resolver. This type resolver restricts the types that can be deserialized to those defined in the Web service’s method signature, or the ones that have the GenerateScriptTypeAttribute applied. You cannot modify this built-in type resolver programmatically.
Perhaps the GenerateScriptType Attribute can help you. But I don't know what kind of __type Properties are be needed here.
A base project contains an abstract base class Foo. In separate client projects, there are classes implementing that base class.
I'd like to serialize and restore an instance of a concrete class by calling some method on the base class:
// In the base project:
public abstract class Foo
{
abstract void Save (string path);
abstract Foo Load (string path);
}
It can be assumed that at the time of deserialization, all needed classes are present. If possible in any way, the serialization should be done in XML. Making the base class implement IXmlSerializable is possible.
I'm a bit stuck here. If my understanding of things is correct, then this is only possible by adding an [XmlInclude(typeof(UnknownClass))] to the base class for every implementing class - but the implementing classes are unknown!
Is there a way to do this? I've got no experience with reflection, but i also welcome answers using it.
Edit: The problem is Deserializing. Just serializing would be kind of easy. :-)
You can also do this at the point of creating an XmlSerializer, by providing the additional details in the constructor. Note that it doesn't re-use such models, so you'd want to configure the XmlSerializer once (at app startup, from configuration), and re-use it repeatedly... note many more customizations are possible with the XmlAttributeOverrides overload...
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
static class Program
{
static readonly XmlSerializer ser;
static Program()
{
List<Type> extraTypes = new List<Type>();
// TODO: read config, or use reflection to
// look at all assemblies
extraTypes.Add(typeof(Bar));
ser = new XmlSerializer(typeof(Foo), extraTypes.ToArray());
}
static void Main()
{
Foo foo = new Bar();
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, foo);
ms.Position = 0;
Foo clone = (Foo)ser.Deserialize(ms);
Console.WriteLine(clone.GetType());
}
}
public abstract class Foo { }
public class Bar : Foo {}
You don't have to put the serialization functions into any base class, instead, you can add it to your Utility Class.
e.g. ( the code is for example only, rootName is optional )
public static class Utility
{
public static void ToXml<T>(T src, string rootName, string fileName) where T : class, new()
{
XmlSerializer serializer = new XmlSerializer(typeof(T), new XmlRootAttribute(rootName));
XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.UTF8);
serializer.Serialize(writer, src);
writer.Flush();
writer.Close();
}
}
Simply make call to
Utility.ToXml( fooObj, "Foo", #"c:\foo.xml");
Not only Foo's family types can use it, but all other serializable objects.
EDIT
OK full service... (rootName is optional)
public static T FromXml<T>(T src, string rootName, string fileName) where T : class, new()
{
XmlSerializer serializer = new XmlSerializer(typeof(T), new XmlRootAttribute(rootName));
TextReader reader = new StreamReader(fileName);
return serializer.Deserialize(reader) as T;
}
Well the serialization shouldn't be a problem, the XmlSerializer constructor takes a Type argument, even calling GetType on an instance of a derived class through a method on the abstract base will return the derived types actual Type. So in essence as long as you know the proper type upon deserialization then the serialization of the proper type is trivial. So you can implement a method on the base called serialize or what have you that passes this.GetType() to the constructor of the XmlSerializer.. or just passes the current reference out and lets the serialize method take care of it and you should be fine.
Edit: Update for OP Edit..
If you don't know the type at deserialization then you really have nothing but a string or byte array, without some sort of identifier somewhere you are kind of up a creek. There are some things you can do like trying to deserialize as every known derived type of the xx base class, I would not recommend this.
Your other option is to walk the XML manually and reconstruct an object by embedding the type as a property or what have you, maybe that is what you originally meant in the article, but as it stands I don't think there is a way for the built in serialization to take care of this for you without you specifying the type.
Somewhere deep inside the XML namespaces lies a wonderful class called XmlReflectionImporter.
This may be of help to you if you need to create a schema at runtime.
You can also do this by creating an XmlSerializer passign in all possible types to the constructor. Be warned that when you use this constructor the xmlSerializer will be compiled each and every time and will result in a leak if you constantly recreate it. You will want to create a single serializer and reuse it in your application.
You can then bootstrap the serializer and using reflection look for any descendants of foo.
These links will probably be helpful to you:
CodeProject article
Blog post
Stack Overflow question
I have a complex remoting project and wanted very tight control over the serialized XML. The server could receive objects that it had no idea how to deserialize and vice versa, so I needed a way to identify them quickly.
All of the .NET solutions I tried lacked the needed flexibility for my project.
I store an int attribute in the base xml to identify the type of object.
If I need to create a new object from xml I created a factory class that checks the type attribute then creates the appropriate derived class and feeds it the xml.
I did something like this (pulling this out of memory, so syntax may be a little off):
(1) Created an interface
interface ISerialize
{
string ToXml();
void FromXml(string xml);
};
(2) Base class
public class Base : ISerialize
{
public enum Type
{
Base,
Derived
};
public Type m_type;
public Base()
{
m_type = Type.Base;
}
public virtual string ToXml()
{
string xml;
// Serialize class Base to XML
return string;
}
public virtual void FromXml(string xml)
{
// Update object Base from xml
}
};
(3) Derived class
public class Derived : Base, ISerialize
{
public Derived()
{
m_type = Type.Derived;
}
public override virtual string ToXml()
{
string xml;
// Serialize class Base to XML
xml = base.ToXml();
// Now serialize Derived to XML
return string;
}
public override virtual void FromXml(string xml)
{
// Update object Base from xml
base.FromXml(xml);
// Update Derived from xml
}
};
(4) Object factory
public ObjectFactory
{
public static Base Create(string xml)
{
Base o = null;
Base.Type t;
// Extract Base.Type from xml
switch(t)
{
case Base.Type.Derived:
o = new Derived();
o.FromXml(xml);
break;
}
return o;
}
};
This method reads the XML root element and checks if the current executing assembly contains a type with such a name. If so, the XML document is deserialized. If not, an error is thrown.
public static T FromXml<T>(string xmlString)
{
Type sourceType;
using (var stringReader = new StringReader(xmlString))
{
var rootNodeName = XElement.Load(stringReader).Name.LocalName;
sourceType =
Assembly.GetExecutingAssembly().GetTypes()
.FirstOrDefault(t => t.IsSubclassOf(typeof(T))
&& t.Name == rootNodeName)
??
Assembly.GetAssembly(typeof(T)).GetTypes()
.FirstOrDefault(t => t.IsSubclassOf(typeof(T))
&& t.Name == rootNodeName);
if (sourceType == null)
{
throw new Exception();
}
}
using (var stringReader = new StringReader(xmlString))
{
if (sourceType.IsSubclassOf(typeof(T)) || sourceType == typeof(T))
{
var ser = new XmlSerializer(sourceType);
using (var xmlReader = new XmlTextReader(stringReader))
{
T obj;
obj = (T)ser.Deserialize(xmlReader);
xmlReader.Close();
return obj;
}
}
else
{
throw new InvalidCastException(sourceType.FullName
+ " cannot be cast to "
+ typeof(T).FullName);
}
}
}
Marking the classes as Serializable and using SoapBinaryFormatter instead of XmlSerializer will give you this functionality automatically. When serializing the type information of the instance being serialized will be written to the XML, and SoapBinaryFormatter can instantiate the subclasses when deserializing.
I used the XmlType attribute of the unknown (but expected) classes to determine the Type for the deserialization. The to be expected types are load during the instantiation of the AbstractXmlSerializer class and placed in a dictionary. During the deserialization the root element is read and with this the type is retrieved form the dictionary. After that it can be deserialized normally.
XmlMessage.class:
public abstract class XmlMessage
{
}
IdleMessage.class:
[XmlType("idle")]
public class IdleMessage : XmlMessage
{
[XmlElement(ElementName = "id", IsNullable = true)]
public string MessageId
{
get;
set;
}
}
AbstractXmlSerializer.class:
public class AbstractXmlSerializer<AbstractType> where AbstractType : class
{
private Dictionary<String, Type> typeMap;
public AbstractXmlSerializer(List<Type> types)
{
typeMap = new Dictionary<string, Type>();
foreach (Type type in types)
{
if (type.IsSubclassOf(typeof(AbstractType))) {
object[] attributes = type.GetCustomAttributes(typeof(XmlTypeAttribute), false);
if (attributes != null && attributes.Count() > 0)
{
XmlTypeAttribute attribute = attributes[0] as XmlTypeAttribute;
typeMap[attribute.TypeName] = type;
}
}
}
}
public AbstractType Deserialize(String xmlData)
{
if (string.IsNullOrEmpty(xmlData))
{
throw new ArgumentException("xmlData parameter must contain xml");
}
// Read the Data, Deserializing based on the (now known) concrete type.
using (StringReader stringReader = new StringReader(xmlData))
{
using (XmlReader xmlReader = XmlReader.Create(stringReader))
{
String targetType = GetRootElementName(xmlReader);
if (targetType == null)
{
throw new InvalidOperationException("XML root element was not found");
}
AbstractType result = (AbstractType)new
XmlSerializer(typeMap[targetType]).Deserialize(xmlReader);
return result;
}
}
}
private static string GetRootElementName(XmlReader xmlReader)
{
if (xmlReader.IsStartElement())
{
return xmlReader.Name;
}
return null;
}
}
UnitTest:
[TestMethod]
public void TestMethod1()
{
List<Type> extraTypes = new List<Type>();
extraTypes.Add(typeof(IdleMessage));
AbstractXmlSerializer<XmlMessage> ser = new AbstractXmlSerializer<XmlMessage>(extraTypes);
String xmlMsg = "<idle></idle>";
MutcMessage result = ser.Deserialize(xmlMsg);
Assert.IsTrue(result is IdleMessage);
}