Home All Groups Group Topic Archive Search About

Custom Type for Application Settings

Author
13 Jul 2006 11:05 PM
argo.howser
The MSDN doc says that I can use any type that is "XML Serializable" as
a type for an application setting. I can't get it to work! The IDE
(Settings tab in Project Properties) always tells me "Cannot be
converted to an instance of type EncryptedString".

What am I missing?

    [Serializable]
    [XmlRootAttribute()]
    public class EncryptedString : IXmlSerializable
    {
        public string EncryptedValue = "";

        public EncryptedString()
        {

        }

        public EncryptedString(string value)
        {
            EncryptedValue = Encryptor.RC2Encrypt(value);
        }

        public override string ToString()
        {
            return EncryptedValue;
        }

        public static implicit operator EncryptedString(string value)
        {
            return new EncryptedString(value);
        }

        public static implicit operator string(EncryptedString value)
        {
            return value.UnencryptedValue;
        }

        public string UnencryptedValue
        {
            get
            {
                return Encryptor.RC2Decrypt(EncryptedValue);
            }
            set
            {
                EncryptedValue = Encryptor.RC2Encrypt(value);
            }
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            reader.MoveToContent();

            if (reader.Read())
                EncryptedValue = reader.Value;
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteElementString("EncryptedValue",
EncryptedValue);
        }

        #endregion
    }

AddThis Social Bookmark Button