I have the following Type:
Class Command<TData> : Base Where TData : I
In runtime this class is build (with specific TDATA) and Serialized.
I have 2 issues( I’m using V2 ):
1) When I’m adding this type to runtime modal :
var meta = this._modal.Add(type, false)
.Add(this.GetDMProperties(type).Select(p => p.Name)
.ToArray());
I get the following exception:
Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
at ProtoBuf.Meta.TypeModel.ResolveProxies(Type type) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 952
at ProtoBuf.Meta.RuntimeTypeModel.FindWithoutAdd(Type type) in C:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 118
at ProtoBuf.Meta.ValueMember..ctor(RuntimeTypeModel model, Type parentType, Int32 fieldNumber, MemberInfo member, Type memberType, Type itemType, Type defaul
tType, DataFormat dataFormat, Object defaultValue) in C:\Dev\protobuf-net\protobuf-net\Meta\ValueMember.cs:line 75
at ProtoBuf.Meta.MetaType.AddField(Int32 fieldNumber, String memberName, Type itemType, Type defaultType, Object defaultValue) in C:\Dev\protobuf-net\protobu
f-net\Meta\MetaType.cs:line 1165
at ProtoBuf.Meta.MetaType.Add(String[] memberNames) in C:\Dev\protobuf-net\protobuf-net\Meta\MetaType.cs:line 1046
2) If i try to skip it at start up and try to do it on demand like below:
if (this._modal.CanSerializeContractType(objectType) == false)
{
this._modal.Add(objectType, false);
this._modal.CompileInPlace();
}
CanSerializeContractType returns true, but in practice only the base data is serialized.
My question is what is the practice of adding this type to modal without adding all permutations on design time if possible ?
I will have to investigate the first; sounds like an odd edge case bug. However, your second point ties into a conversation I had a short while ago – with the conclusion that I will add an event that is fired when a type is first seen. You would then have the opportunity during the event of configuring the type. In theory: simple; I just need to implement it. I will “bump” this forwards on my list of things…
Side note: unless you are only working inside a single AppDomain and not writing to disk or sending over the network or between processes, you must make sure you can reliably add properties/sun-types with the same keys. Or more specifically: eithrr your GetDMaproperties method (not shown) needs some kind of fixed order, or you need an OrderBy in the LINQ. Also; that is brittle – if anyone adds/removes/renames a property, the keys will change.