Linq-to-SQL overriding my OnSerializing attribute

140 views Asked by At

My Code

partial class User
{
    [OnSerializing]
    public void ClearPassword()
    {
        Password = null;
    }
}

Linq-to-SQL

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[User]")]
[global::System.Runtime.Serialization.DataContractAttribute()]
public partial class User : INotifyPropertyChanging, INotifyPropertyChanged
{
    [global::System.Runtime.Serialization.OnSerializingAttribute()]
    [global::System.ComponentModel.EditorBrowsableAttribute(EditorBrowsableState.Never)]
    public void OnSerializing(StreamingContext context)
    {
        this.serializing = true;
    }
}

Result

Invalid attribute. Both 'Void ClearPassword()' and 'Void OnSerializing(System.Runtime.Serialization.StreamingContext)' in type 'AuthenticationManager.User' have 'System.Runtime.Serialization.OnSerializingAttribute'.

Now, did the engineers at Microsoft create a way for two separate blocks of code to fire some events on serialization independent of one another? Especially considering the fact that they hijack this event to set this.serializing = true?

Thanks in advance.

1

There are 1 answers

2
jessehouwing On

If your Password property is using a backing field, you can set it to NonSerialized

  [XmlIgnore]
  [ScriptIgnore]
  public string Password { get { return _password;}  set { _password = value; } }

  [NonSerialized]
  private string _password;