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?

6
  • You can't. Its not clear the reason you think you need to uninitialize your object Commented Oct 26, 2012 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. Commented Oct 26, 2012 at 15:24
  • Iirc, on .netcore DataContractSerializer (and friends) do run a parameterless constructor. I could be wrong, though. Commented Oct 26, 2012 at 15:28
  • @ramhound that isn't to "uninitialize" an object - it is to create an object without initializing it - pretty common in serialization. Commented Oct 26, 2012 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. Commented Oct 26, 2012 at 15:46

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.