Why do we need a constructor without parameters?

Possible duplicate:
Why the XML-Serializable class needs a parameterless constructor

I get a runtime error as below.

Message = OutlookAddIn1.DeviceRegistrationRequest cannot be serialized because it does not have a constructor without parameters.

I completely understand why (said in the error message) and how to solve it (trivial addition of an empty constructor). What I do not understand is why it is required. I found this discussion , but mostly it concerns MVC, which has nothing to do with my program (which is a console client for CRM Dynamics).

+4
source share
6 answers

There is nothing about MVC (Sorry, I read your post incorrectly). It is only about creating a simple C # 'py object. You see, take this class as an example:

public class Why { public Why(int x, int y) { } } 

How does the deserializer know what to pass when your object is deserialized and built? He could not guess. Thus, the framework requires serializable objects to have constructors without parameters, so it’s safe to β€œjust create” your responsibility . make all state accessible through properties.

note: by the way - note that the constructor should not be publicly available. Most serializers do very well with private constructors with no parameters or nothing at all if they are implemented to use an uninitialized object construct accessible from Reflection at least in the full .Net profile.

+2
source

In your class, you only need a constuctor without parameters, because the library you are using (sounds like an XmlSerializer , perhaps indirectly) expects and uses this constructor. This is a really convenient way to create objects, because it allows you to:

 object obj = Activator.CreateInstance(type); 

using.

But! This is not an essential requirement for all serializers:

  • there are serializers that do not use any constructor (there is another way to create objects that completely skips the constructor step)
  • serializers exist that allow you to provide your own factory method for creating new instances
+6
source

It is required that code that knows nothing about parameterized constructors can build one of your objects based on the agreement that a constructor without parameters is available.

Deserialization requires an instance of an object, so the deserialization process will create it using this constructor.

+1
source

For deserialization to create an instance. You can make a private or internal constructor if you want to hide it.

+1
source

Do not specify the details of the problem, but usually say:

In order to build an object of type type, you will need ctor by default . If you do not have it, there is no way to do this. There is no way to figure out the correct parameter to go to ctor s parameters, so it does not clear the "clear" parameter-less ctor.

0
source

This is because the deserializer should be able to simplify creating an instance of the class and populating it with data.

If there is no constructor without parameters, the deserializer must guess which parameters to send to the constructor. This could work quite well if there was an agreement on what parameters the constructor should have, but the simplest convention is that there should be a constructor without parameters.

0
source

Source: https://habr.com/ru/post/1435869/


All Articles