当想要序列化肥皂时如何强制设置某个成员值

发布于 2024-12-06 02:35:24 字数 947 浏览 1 评论 0原文

我有这样的方法:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼中杀气 2024-12-13 02:35:24

创建一个附加的无参数构造函数并将其标记为 Obsolete(“仅用于序列化”, true)。
这将允许编译器使用构造函数,但是当您尝试使用此构造函数时,即使使用继承,您的用户代码也不会编译:

public abstract class MyBaseClass
{
    public bool Success { get; set; }

    [Obsolete("only for serialization", true)]
    public MyBaseClass(){}

    public MyBaseClass(bool success) { this.Success = success; }
}

public class MyConcreteClass
{
    [Obsolete("only for serialization", true)]
    public MyConcreteClass() : base() {}

    public MyBaseClass(bool success) : base(success) { /* whatever in this instance */ }
}

您甚至可以省略基本构造函数:

public abstract class MyBaseClass
{
    public bool Success { get; set; }

    public MyBaseClass(bool success) { this.Success = success; }
}

public class MyConcreteClass
{
    [Obsolete("only for serialization", true)]
    /* calling base constructor with false success parameter */
    public MyConcreteClass() : base(false) {}

    public MyBaseClass(bool success) : base(success) { /* whatever in this instance */ }
}

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:

public abstract class MyBaseClass
{
    public bool Success { get; set; }

    [Obsolete("only for serialization", true)]
    public MyBaseClass(){}

    public MyBaseClass(bool success) { this.Success = success; }
}

public class MyConcreteClass
{
    [Obsolete("only for serialization", true)]
    public MyConcreteClass() : base() {}

    public MyBaseClass(bool success) : base(success) { /* whatever in this instance */ }
}

you can even leave out the base constructor:

public abstract class MyBaseClass
{
    public bool Success { get; set; }

    public MyBaseClass(bool success) { this.Success = success; }
}

public class MyConcreteClass
{
    [Obsolete("only for serialization", true)]
    /* calling base constructor with false success parameter */
    public MyConcreteClass() : base(false) {}

    public MyBaseClass(bool success) : base(success) { /* whatever in this instance */ }
}
_畞蕅 2024-12-13 02:35:24

您需要有一个无参数构造函数才能序列化。但您希望每个 Result 都具有 Success = true 吗?

public OpenAccountResult()
{
    Success = true;
}

或者,如果它不是始终 true,并且您想指定Success,那么您可以简单地使用两个构造函数:

// required for serialization
public OpenAccountResult()
{
}

// alternate constructor for you to use
public OpenAccountResult(bool successValue)
{
    Success = successValue;
}

如果您不想设置每个Result 构造函数,然后在基类中执行。如果基类没有 Success 属性,则创建一个子类,所有结果类都可以从中派生:

public abstract class Result : BaseSoapAnswer
{
    public Result()
    {
        this.Success = true;
    }

    public bool Success { get; set; }
}

public class OpenAccountResult : Result 
// ...
public class AnotherResult : Result 

You need to have a parameter-less constructor in order to be serialized. But you want every Result to have Success = true?

public OpenAccountResult()
{
    Success = true;
}

Or if it's not always true and you want to specify Success, then you can simply have two constructors:

// required for serialization
public OpenAccountResult()
{
}

// alternate constructor for you to use
public OpenAccountResult(bool successValue)
{
    Success = successValue;
}

If you don't want to set every Result constructor, then do it in the base class. If the base class doesn't have a Success property, then make a subclass from which all your result classes can derive:

public abstract class Result : BaseSoapAnswer
{
    public Result()
    {
        this.Success = true;
    }

    public bool Success { get; set; }
}

public class OpenAccountResult : Result 
// ...
public class AnotherResult : Result 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文