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.
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: