If you can't specify attributes under your model, you can go with second approach to fill RuntimeTypeModel
at application start. Here is a sample given my Marc Gravell - Protobuf-net serialization without annotation
Code sample for your EFTable
:
RuntimeTypeModel.Default.Add(typeof(EFTable), false).Add("str", "num",);
Or you can fill it with reflection based on your EFTable properties, and then it should serialize correctly.
But beware, as Protobuf is critical to properties order. And if you fill this RuntimeTypeModel
object dynamically by reflection, and then you will add new property, then data that was serialized with previous version, won`t work in new version, as properties order will be changed.
UPDATE1
As I mentioned you can also use reflection to populate fields dynamically. Here is a sample of such method:
public static void Generate(IEnumerable<Type> types)
{
var model = RuntimeTypeModel.Default;
foreach (var type in types)
{
int counter = 1;
var metaType = model.Add(type, false);
var properties = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
foreach (var propertyInfo in properties)
{
metaType.Add(counter++, propertyInfo.Name);
}
}
}
And usage sample:
Generate(new List<Type>() { // list of objects that should be registered with ProtoBuf-Net
typeof(ASPStateTempSession),
typeof(ASPStateTempApplication)
});
But as i previously said, if you add new property in new version of your application, then old saved cached data won't work, as order of properties will be changed.