2
votes

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 ?

1
Hi again; I've tried to repro your 2 issues (the exception, and the issue with CanSerialize...) - cannot reproduce either - here's my test - can you add any context to help me repro? Re the second, I wonder if CanSerialize... reports false initially, then you Add it, but you haven't added any members to serialize - is this the issue?Marc Gravell♦
I added the code <script src='pastie.org/3327015.js'></script> CanSerialize returned true event though i didnt add the type.eyan
that snippet does not allow me to reproduce the scenario - in particular the entire "Types" assembly. I could fake things, but then I can't guarantee that I'm looking at the same problem. Is there any way of cutting this down, maybe with just a single concrete class, that I can use to reproduce the issue?Marc Gravell♦
link the code, the last is the types assembly.if there is anything else i can do to help you repro,please let me knoweyan
k; I have a repro for that now, thanks - it relates to open generic types (type == T, for no specific T). I'll see what I can do (fixing that it easy - I mean more: thinking of a way to address the scenario)Marc Gravell♦

1 Answers

0
votes

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.