/// <summary> /// Test whether this StatusInstance's status is a valid value for the type of the "status" argument. /// If so, load its value into "status" and return true. /// (For enums, test whether this value is in the defined range for that enum.) /// </summary> public bool TryGetStatus <TStatus>(out TStatus status) where TStatus : struct { if (StatusConverter <TBaseStatus, TStatus> .Convert != null) { status = StatusConverter <TBaseStatus, TStatus> .Convert(this.Status); } else { try { status = (TStatus)(object)this.Status; } catch (InvalidCastException) { status = default(TStatus); return(false); } } if (typeof(TStatus).IsEnum) { try { return(Enum.IsDefined(typeof(TStatus), this.Status)); } catch (ArgumentException) { return(false); } } return(true); // I guess this should return true. If it isn't an enum, all we know is that the cast was successful. }
protected static TBaseStatus Convert <TStatus>(TStatus status) where TStatus : struct { try { return(StatusConverter <TStatus, TBaseStatus> .Convert(status)); } catch (NullReferenceException) { string tName = typeof(TStatus).Name; string baseName = typeof(TBaseStatus).Name; throw new InvalidOperationException($"No converter found for {tName}. (StatusConverter<{tName}, {baseName}>.Convert must be given a value before defining status rules.)"); } }
/// <summary> /// Casts this instance's Status property to a chosen type, regardless of whether it falls into the defined range of enum types. /// </summary> public TStatus GetStatus <TStatus>() { if (StatusConverter <TBaseStatus, TStatus> .Convert != null) { return(StatusConverter <TBaseStatus, TStatus> .Convert(this.Status)); } try { return((TStatus)(object)this.Status); } catch (InvalidCastException) { throw new InvalidOperationException($"Couldn't convert value {this.Status} to type {typeof(TStatus).FullName}"); } }
protected static TBaseStatus Convert <TStatus>(TStatus status) where TStatus : struct { return(StatusConverter <TStatus, TBaseStatus> .Convert(status)); }