当想要序列化肥皂时如何强制设置某个成员值
我有这样的方法:
[WebMethod]
public OpenAccountResult OpenAccount()
{
OpenAccountResult test = new OpenAccountResult(/*true*/)//the true here wont let the object to be serialized
{
//Success = true,
AccountNumber = "test111"
};
return test;
}
OpenAccountResult
实际上是在扩展BaseSoapAnswer
,所以我为BaseSoapAnswer
创建了一个带有bool
参数的BaseSoapAnswer
构造函数code>成功,但出现异常,带有需要参数的构造函数的对象无法序列化。
例外:
[InvalidOperationException: WebService.Services.TradingSystem.OpenAccountResult cannot be serialized because it does not have a parameterless constructor.]
如何强制每个 *Result
实例声明 bool
Success
。 我想通过实现或继承来做到这一点,因为将会有很多 *Result
并且我不希望任何其他程序员忘记它。
最重要的是,我想以每个实例都必须设置 Success 的方式构建 *Result,如果忘记了,出于安全原因,代码将不会被编译(您可能会明白,如果我不需要序列化我不会有问题)。
谢谢。
I have this method:
[WebMethod]
public OpenAccountResult OpenAccount()
{
OpenAccountResult test = new OpenAccountResult(/*true*/)//the true here wont let the object to be serialized
{
//Success = true,
AccountNumber = "test111"
};
return test;
}
OpenAccountResult
is actually extending BaseSoapAnswer
, so I made BaseSoapAnswer
constructor with one bool
parameter for Success
, but I got an exception that an object with constructor that needs a parameter cannot be serialized.
Exception:
[InvalidOperationException: WebService.Services.TradingSystem.OpenAccountResult cannot be serialized because it does not have a parameterless constructor.]
How do I force every *Result
instance to declare bool
Success
.
I want to do it by implementing or inheriting because it there are going to be a lot of *Result
and I don't want any other programmer to forget about it.
In the bottom line, I want to build the *Result in the way that every instance will have to set the Success, and if it is forgotten, the code would not be compiled, for safety reasons (You probably understand that if I would not need to be serialized I would not have a problem).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个附加的无参数构造函数并将其标记为 Obsolete(“仅用于序列化”, true)。
这将允许编译器使用构造函数,但是当您尝试使用此构造函数时,即使使用继承,您的用户代码也不会编译:
您甚至可以省略基本构造函数:
Create an additional parameterless constructor and mark it as Obsolete("only for serialization", true).
This will allow the compiler to use the constructor, but your user code will not compile when you are trying to use this constructor, even with inheritance:
you can even leave out the base constructor:
您需要有一个无参数构造函数才能序列化。但您希望每个
Result
都具有Success = true
吗?或者,如果它不是始终 true,并且您想指定
Success
,那么您可以简单地使用两个构造函数:如果您不想设置每个
Result 构造函数,然后在基类中执行。如果基类没有
Success
属性,则创建一个子类,所有结果类都可以从中派生:You need to have a parameter-less constructor in order to be serialized. But you want every
Result
to haveSuccess = true
?Or if it's not always true and you want to specify
Success
, then you can simply have two constructors:If you don't want to set every
Result
constructor, then do it in the base class. If the base class doesn't have aSuccess
property, then make a subclass from which all your result classes can derive: