简体   繁体   English

C# 序列化错误

[英]C# Serialization error

I'm trying to store object of Employee in Session (DB Based), When i do this I'm getting serialization error "Unable to serialize the session state. In 'StateServer'" I'm trying to store object of Employee in Session (DB Based), When i do this I'm getting serialization error "Unable to serialize the session state. In 'StateServer'"

[Serializable]
public class Employee
{
    ... 
public List<System.ComponenetModel.DataAnnotation.ValidationResult> MyValidationResult
  { get; set; }
}

I figured out it is happening when it tries to serialize MyValidationResult it is happening, Any idea how to fix it?当它试图序列化它正在发生的 MyValidationResult 时,我发现它正在发生,知道如何解决它吗?

You could use theNonSerializedAttribute .您可以使用NonSerializedAttribute Add a backing field and don't use an auto-implemented property and add the NonSerializedAttribute to the field and you should be good to go.添加一个支持字段并且不要使用自动实现的属性并将 NonSerializedAttribute 添加到该字段,您应该对 go 很好。

ValidationResult cannot be serialized because it doesn't have a parameterless constructor. ValidationResult 无法序列化,因为它没有无参数构造函数。 If you need to save the validations in session state, you'll have to come up with another class to do so.如果您需要将验证保存在 session state 中,则必须提供另一个 class 来执行此操作。 If you don't need it, then apply the NonSerialized attribute to the property.如果您不需要它,则将 NonSerialized 属性应用于该属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM