eadred / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

Add support for custom object creation #423

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Email address: jrhodes@redpointsoftware.com.au

Currently protobuf-net uses Activator.CreateInstance to create to complex types 
when they are deserialized.  I can get around this one layer deep by passing an 
instance in, but any further than that and Activator.CreateInstance will be 
called.

I'd like to instead use FormatterServices.GetUninitializedObject, which allows 
construction of objects without calling the constructor.  This method is 
designed specifically for serialization and deserialization of objects.

The best way of implementing this would probably be a general method that takes 
a type and returns an instance of it, and allowing that delegate to be set 
against the RuntimeTypeModel.

Original issue reported on code.google.com by jrho...@redpointsoftware.com.au on 26 Dec 2013 at 11:17

GoogleCodeExporter commented 9 years ago
Firstly, note that for most users it will be using "newobj" via the 
metaprogramming layer - not Activator.CreateInstance.

Secondly, ctor skipping is already fully supported - just put 
SkipCpnstructor=true on the ProtoContractAttribute or via Runtime TypeModel.

Thirdly, it also supports custom type-factories, for any other 
implementation-defined scenario - both per-type and global.

Basically: everything you want is already there. Let me know if you can't find 
it from the above (I'm not at a PC right now, hence no full example).

Original comment by marc.gravell on 27 Dec 2013 at 12:04

GoogleCodeExporter commented 9 years ago
Thanks, the SkipConstructor parameter seems to do what I want.

I was looking at the TypeModel implementation and saw Activator.CreateInstance 
in the deserialization code, and the code looked like that's what it was using 
to construct the passed in value if it didn't exist.

Original comment by jrho...@redpointsoftware.com.au on 27 Dec 2013 at 12:13