diff --git a/ValueOf/ValueOf.cs b/ValueOf/ValueOf.cs index e6e5666..ad6d762 100644 --- a/ValueOf/ValueOf.cs +++ b/ValueOf/ValueOf.cs @@ -1,109 +1,97 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Reflection; - -namespace ValueOf -{ - public class ValueOf where TThis : ValueOf, new() - { - private static readonly Func Factory; - - /// - /// WARNING - THIS FEATURE IS EXPERIMENTAL. I may change it to do - /// validation in a different way. - /// Right now, override this method, and throw any exceptions you need to. - /// Access this.Value to check the value - /// - protected virtual void Validate() - { - } - +using System.Collections.Generic; + +namespace ValueOf +{ + public class ValueOf where TThis : ValueOf, new() + { + /// + /// WARNING - THIS FEATURE IS EXPERIMENTAL. I may change it to do + /// validation in a different way. + /// Right now, override this method, and throw any exceptions you need to. + /// Access this.Value to check the value + /// + protected virtual void Validate() + { + } + protected virtual bool TryValidate() { return true; - } - - static ValueOf() - { - ConstructorInfo ctor = typeof(TThis) - .GetTypeInfo() - .DeclaredConstructors - .First(); - - var argsExp = new Expression[0]; - NewExpression newExp = Expression.New(ctor, argsExp); - LambdaExpression lambda = Expression.Lambda(typeof(Func), newExp); - - Factory = (Func)lambda.Compile(); - } - - public TValue Value { get; protected set; } - - public static TThis From(TValue item) - { - TThis x = Factory(); - x.Value = item; - x.Validate(); - - return x; - } - + } + + static ValueOf() + { + } + + public TValue Value { get; protected set; } + + public static TThis From(TValue item) + { + TThis x = new TThis() + { + Value = item + }; + x.Validate(); + + return x; + } + public static bool TryFrom(TValue item, out TThis thisValue) { - TThis x = Factory(); - x.Value = item; + TThis x = new TThis() + { + Value = item + }; thisValue = x.TryValidate() ? x : null; return thisValue != null; - } - - protected virtual bool Equals(ValueOf other) - { - return EqualityComparer.Default.Equals(Value, other.Value); - } - - public override bool Equals(object obj) - { - if (obj is null) - return false; - - if (ReferenceEquals(this, obj)) - return true; - - return obj.GetType() == GetType() && Equals((ValueOf)obj); - } - - public override int GetHashCode() - { - return EqualityComparer.Default.GetHashCode(Value); - } - - public static bool operator ==(ValueOf a, ValueOf b) - { - if (a is null && b is null) - return true; - - if (a is null || b is null) - return false; - - return a.Equals(b); - } - - public static bool operator !=(ValueOf a, ValueOf b) - { - return !(a == b); - } - - // Implicit operator removed. See issue #14. - - public override string ToString() - { - return Value.ToString(); - } - } -} \ No newline at end of file + } + + protected virtual bool Equals(ValueOf other) + { + return EqualityComparer.Default.Equals(Value, other.Value); + } + + public override bool Equals(object obj) + { + if (obj is null) + return false; + + if (ReferenceEquals(this, obj)) + return true; + + return obj.GetType() == GetType() && Equals((ValueOf)obj); + } + + public override int GetHashCode() + { + return EqualityComparer.Default.GetHashCode(Value); + } + + public static bool operator ==(ValueOf a, ValueOf b) + { + if (a is null && b is null) + return true; + + if (a is null || b is null) + return false; + + return a.Equals(b); + } + + public static bool operator !=(ValueOf a, ValueOf b) + { + return !(a == b); + } + + // Implicit operator removed. See issue #14. + + public override string ToString() + { + return Value.ToString(); + } + } +}