Skip to content

Instantly share code, notes, and snippets.

@abenedykt
Created March 24, 2016 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abenedykt/d4aa47f3e883aebdb16c to your computer and use it in GitHub Desktop.
Save abenedykt/d4aa47f3e883aebdb16c to your computer and use it in GitHub Desktop.
public abstract class StringValueObject<T> where T : StringValueObject<T>
{
public string Value { get; protected set; }
public static bool operator ==(StringValueObject<T> a, StringValueObject<T> b)
{
if (ReferenceEquals(a, b))
{
return true;
}
if (((object)a == null) || ((object)b == null))
{
return false;
}
return a.Equals(b);
}
public static bool operator !=(StringValueObject<T> a, StringValueObject<T> b)
{
return !(a == b);
}
protected bool Equals(StringValueObject<T> other)
{
return Value.Equals(other.Value);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == GetType() && Equals((StringValueObject<T>)obj);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public static T From(string value)
{
var obj = (StringValueObject<T>)Activator.CreateInstance(typeof (T), true);
obj.Value = value;
return obj as T;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment