我为什么需要一个参数的构造函数? [英] Why do I need a parameterless constructor?

查看:125
本文介绍了我为什么需要一个参数的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
为什么XML序列化类需要一个参数的构造函数

我收到了运行时错误如下说明。

I'm getting the run-time error as stated below.

消息= OutlookAddIn1.DeviceRegistrationRequest不能因为它被序列化没有一个参数的构造函数。

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

我完全清楚,为什么(它的错误消息称),以及如何解决它(琐碎除了空构造函数)。什么我并不清楚是的为什么是必需的。我发现
这个讨论,但它主要是关于MVC,这无关我的节目(这是CRM动态控制台客户端)。

I'm perfectly clear why (it's said in the error message) and how to solve it (trivial addition of the empty constructor). What I'm not clear about is why it's required. I've found this discussion but it's mainly about MVC, which has nothing to do with my program (which is a console client for CRM Dynamics).

推荐答案

不,它是一无所知MVC (对不起,我误解你的职位)。它大约只有纯C#PY对象的创建。你看,利用这个类的一个示例:

No it is nothing about MVC (sorry, I've misread your post). It is only about plain C#'py object creation. You see, take this class for an example:

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

将如何解串器的了解什么通过当你的对象进行反序列化和建设?他无法猜测。因此,该框架的要求的序列化对象的必须有参数构造函数,因此它是安全的,刚创造,这是您的责任以。使通过属性可设置全州

How would the deserializer know what to pass when your object is to be deserialized and constructed? He couldn't guess. Thus, the framework requires that serializable objects have to have parameterless constructors, so it is safe to "just create" and it is your responsibility to make whole state settable via properties.

请注意:对了 - 注意,构造的没有被公开的。大多数串行私人参数构造函数或没有做的非常好不惜一切,如果他们实施使用初始化的对象结构,即可以从反思至少净完整的个人资料。

note: by the way - note that the constructor does not have to be public. Most serializers do very well with private parameterless constructors or none at all, if they implemented to use uninitialized object construction, that is available from Reflection in at least the .Net full profile.

这篇关于我为什么需要一个参数的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆