I do not own an XBox (yet), so I cannot do a cross platform test, but as far as I can tell, the .NET framework on the XBox supports enough reflection, so that a universal serializer for the content pipeline could work or am I wrong here?
It could look like this:
public class XNASerializable : Attribute { ... }
public class MyCustomContent
{
[XNASerializable]
public bool BoolProperty { get { ... } set { ... } }
[XNASerializable]
public string StringProperty { get { ... } set { ... } }
}
in the ContentWriter (pseudocode):
// figure out how many properties to write
int counter = 0;
foreach (PropertyInfo propertyInfo in valueType.GetProperties())
{
if (propertyInfo.GetCustomAttributes(typeof(XNASerializable), true).Length > 0)
counter++;
}
output.Write(counter);
// write the properties
Type valueType = value.GetType();
foreach (PropertyInfo propertyInfo in valueType.GetProperties())
{
if (propertyInfo.GetCustomAttributes(typeof(XNASerializable), true).Length > 0)
{
output.Write(propertyInfo.PropertyName);
output.Write(propertyInfo.PropertyType.AssemblyQualifiedName);
output.Write(propertyInfo.GetValue(value, null));
}
}
in the ContentReader (pseudocode):
int count = input.ReadInt32();
for (int i=0; i<count; i++)
{
PropertyInfo propertyInfo = value.GetType().GetProperty(input.ReadString());
string propertyTypeName = input.ReadString();
switch (typeName)
{
case "System.Boolean":
propertyInfo.SetValue(value, input.ReadBoolean(), null);
break;
case "System.String":
propertyInfo.SetValue(value, input.ReadString(), null);
break;
case ...
break;
default:
object propertyValue = Activator.CreateInstance(propertyInfo.PropertyType);
...
break;
}
}
Well, you get the idea...
Arrays would be easy, too, but you would probably have to jump through some hoops for complex types as the MakeGenericType and so forth is missing in the framework, so the whole serialization/deserialization can be recursive, but cannot call any of the input.ReadObject<...> methods, instead it has to rely on the Activator.CreateInstance and then a recursive call to the same deserialization/serialization routine. The requirements for this to work are exactly the same as for XML serialization, the properties must be read/write and there must be a parameterless default constructor.
Writing one method like this would kind of support our laziness ;) Is there any particular reason this would not work on the XBox?