Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 103 lines (84 sloc) 2.598 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
using System;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace MiscUtils
{
[Serializable]
    public abstract class StringEquivalent<T> : ISerializable, IXmlSerializable where T : StringEquivalent<T>
    {
        protected string Value;

        protected StringEquivalent(string value)
        {
            Ensure.ArgumentNotNull(value, "value");
            Value = value;
        }

        protected StringEquivalent()
        {
        }

        public abstract T Combine(string addition);

        public static T operator +(StringEquivalent<T> a, string b)
        {
            return a.Combine(b);
        }

        public static bool operator ==(StringEquivalent<T> a, StringEquivalent<T> b)
        {
            // If both are null, or both are same instance, return true.
            if (ReferenceEquals(a, b))
            {
                return true;
            }

            // If one is null, but not both, return false.
            if (((object)a == null) || ((object)b == null))
            {
                return false;
            }

            // Return true if the fields match:
            return a.Value.Equals(b.Value, StringComparison.OrdinalIgnoreCase);
        }

        public static bool operator !=(StringEquivalent<T> a, StringEquivalent<T> b)
        {
            return !(a == b);
        }

        public override bool Equals(Object obj)
        {
            return obj != null && Equals(obj as T) || Equals(obj as string);
        }

        public bool Equals(T stringEquivalent)
        {
            return this == stringEquivalent;
        }

        public override int GetHashCode()
        {
            return (Value ?? "").GetHashCode();
        }

        public virtual bool Equals(string other)
        {
            return other != null && Value == other;
        }

        public override string ToString()
        {
            return Value;
        }

        protected StringEquivalent(SerializationInfo info) : this(info.GetValue("Value", typeof (string)) as string)
        {
        }

        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Value", Value);
        }

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            Value = reader.ReadString();
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteString(Value);
        }
    }
}
Something went wrong with that request. Please try again.