4

I want to implement some non-standard serialization format.

When I do deserialization I need to create the instance of an object without calling constructors.

There is
System.Runtime.Serialization.FormatterServices.GetUninitializedObject(Type type) in .NET Framework but there is no FormatterServices class in Windows Metro Framework.

Does any body know how to instantiate object without calling to constructor in Windows Metro Framework?

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
Alexander Eroma
  • 497
  • 3
  • 9
  • **You can't.** Its not clear the reason you think you need to uninitialize your object – Security Hound Oct 26 '12 at 15:10
  • @Ramhound, the reason is that I don't know which constructor to invoke if there is no default constructor. I suppose that `System.Runtime.Serialization.Json.DataContractJsonSerializer` works in the same way, because when it deserialize object, it doesn't call any constructors. – Alexander Eroma Oct 26 '12 at 15:24
  • Iirc, on .netcore DataContractSerializer (and friends) *do* run a parameterless constructor. I could be wrong, though. – Marc Gravell Oct 26 '12 at 15:28
  • @ramhound that isn't to "uninitialize" an object - it is to create an object without initializing it - pretty common in serialization. – Marc Gravell Oct 26 '12 at 15:30
  • @alexander out of curiosity, what is the format? Is it something that would be of general use? Or is it specific to your scenario? The reason I ask: I am deeply involved in serialization code, and I have existing techniques / tools for targeting .netcore and other platforms. If it would be useful to other devs, I might be able to find some time to help you put together a F/OSS library for the purpose (gratuitously stealing large chunks of the protobuf-net source). If not, well, you still might find some of that code useful for reference. – Marc Gravell Oct 26 '12 at 15:46
  • @MarcGravell, thank for you answer. I work on the library, the purpose of which, is to provide simplified api for [MIME-DIR](http://tools.ietf.org/html/rfc2425) based types implementation, such as [vCard](http://en.wikipedia.org/wiki/VCard) or [VCalendar](http://en.wikipedia.org/wiki/ICalendar). The idea is that user writes: `[MimeRecordEntry(Name = "PHOTO")] public Photo Photo { get; set; }` and serializer transform it to: `PHOTO;ENCODING=b;TYPE=JPEG:1h...N0` – Alexander Eroma Oct 26 '12 at 16:17

2 Answers2

2

As far as I know: you can't. You also can't call a private/protected/internal constructor: .netcore has restricted reflection (like the Silverlight model, but with the added pain of GetTypeInfo()).

The only option I can suggest is maybe provide separate support for a user-supplied factory method that does the appropriate things, or similarly the ability to pass in a factory interface. This takes construction away from the serialization engine, and puts it back in control of code that knows about the specific types.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

i may be wrong but when you instanciate an object you must, at least, run a constructor. Even if no constructor is declared, a default empty constructor is called. Instanciation makes a class become an object.

So to me your question has no answer.

Maybe you could build a default empty constructor at runtime if needed. Take a look here

Community
  • 1
  • 1
Lionel D
  • 317
  • 1
  • 10
  • The OP is correct: in regular .net you can indeed create an instance without executing any constructors. This is atypical, and is usually only used by library code like serialization engines, RPC/proxy tools, and database ORM tools. The real question is: "this feature that exists in regular .net: does it exist in .netcore?". Also, you can't add constructors to existing types via reflection, and reflection-emit doesn't exist **at all** in .netcore. – Marc Gravell Oct 26 '12 at 15:41