public static void UnderlyingTypeMatches(Type svo, SingleValueObjectAttribute attr) { var m_Value = svo.GetField("m_Value", BindingFlags.Instance | BindingFlags.NonPublic); Assert.IsNotNull(m_Value, "{0} should contain a m_Value field.", svo); Assert.AreEqual(attr.UnderlyingType, m_Value.FieldType, "{0}.m_Field", svo); }
public static void ParseMatches(Type svo, SingleValueObjectAttribute attr) { var staticMethods = svo.GetMethods(BindingFlags.Public | BindingFlags.Static); var parse = staticMethods.SingleOrDefault(method => method.Name == "Parse" && method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(string) && method.ReturnType == svo); var parseCulture = staticMethods.SingleOrDefault(method => method.Name == "Parse" && method.GetParameters().Length == 2 && method.GetParameters()[0].ParameterType == typeof(string) && method.GetParameters()[1].ParameterType == typeof(IFormatProvider) && method.ReturnType == svo); if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.Parse)) { Assert.IsNotNull(parse, "{0} should contain a static Parse method.", svo); if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.CultureDependent)) { Assert.IsNotNull(parseCulture, "{0} should contain a static Parse method with an IFormatProvider parameter.", svo); } else { Assert.IsNull(parseCulture, "{0} should not contain a static Parse method with an IFormatProvider parameter.", svo); } } else { Assert.IsNull(parse, "{0} should not contain a static Parse method.", svo); Assert.IsNull(parseCulture, "{0} should not contain a static Parse method an IFormatProvider parameter.", svo); } }
/// <summary>Gets the cast needed for casting to the database type.</summary> /// <exception cref="InvalidCastException"> /// If the required cast is not defined. /// </exception> private static MethodInfo GetCast(Type sourceType, SingleValueObjectAttribute attr) { if (!Casts.TryGetValue(sourceType, out MethodInfo cast)) { var returnType = attr.DatabaseType ?? attr.UnderlyingType; var methods = sourceType.GetMethods(BindingFlags.Public | BindingFlags.Static) .Where(m => m.IsHideBySig && m.IsSpecialName && m.GetParameters().Length == 1 && m.ReturnType == returnType) .ToList(); if (methods.Any()) { cast = methods[0]; Casts[sourceType] = cast; } else { throw new InvalidCastException(string.Format(QowaivMessages.InvalidCastException_FromTo, sourceType, returnType)); } } return(cast); }
public void Ctor_Params_AreEqual() { var act = new SingleValueObjectAttribute(SingleValueStaticOptions.All, typeof(String)); Assert.AreEqual(SingleValueStaticOptions.All, act.StaticOptions, "act.StaticOptions"); Assert.AreEqual(typeof(String), act.UnderlyingType, "act.UnderlyingType"); }
/// <summary>Gets the cast needed for casting to the database type.</summary> /// <exception cref="InvalidCastException"> /// If the required cast is not defined. /// </exception> private static MethodInfo GetCast(Type sourceType, SingleValueObjectAttribute attr) { MethodInfo cast; if (!Casts.TryGetValue(sourceType, out cast)) { var returnType = attr.DatabaseType ?? attr.UnderlyingType; var methods = sourceType.GetMethods(BindingFlags.Public | BindingFlags.Static) .Where(m => m.IsHideBySig && m.IsSpecialName && m.GetParameters().Length == 1 && m.ReturnType == returnType) .ToList(); if (methods.Any()) { cast = methods[0]; Casts[sourceType] = cast; } else { throw new InvalidCastException(string.Format(QowaivMessages.InvalidCastException_FromTo, sourceType, returnType)); } } return cast; }
public void Ctor_Params_AreEqual() { var act = new SingleValueObjectAttribute(SingleValueStaticOptions.All, typeof(string)); Assert.AreEqual(SingleValueStaticOptions.All, act.StaticOptions, "act.StaticOptions"); Assert.AreEqual(typeof(string), act.UnderlyingType, "act.UnderlyingType"); }
public static void EmptyAndUnknownMatches(Type svo, SingleValueObjectAttribute attr) { var emptyValue = svo.GetFields(BindingFlags.Static | BindingFlags.Public).SingleOrDefault(field => field.Name == "Empty" && field.IsInitOnly && field.FieldType == svo); var unknownValue = svo.GetFields(BindingFlags.Static | BindingFlags.Public).SingleOrDefault(field => field.Name == "Unknown" && field.IsInitOnly && field.FieldType == svo); var isEmptyMethod = svo.GetMethods(BindingFlags.Instance | BindingFlags.Public).SingleOrDefault(method => method.Name == "IsEmpty" && method.GetParameters().Length == 0 && method.ReturnType == typeof(Boolean)); var isUnknownMethod = svo.GetMethods(BindingFlags.Instance | BindingFlags.Public).SingleOrDefault(method => method.Name == "IsUnknown" && method.GetParameters().Length == 0 && method.ReturnType == typeof(Boolean)); var isEmptyOrUnknownMethod = svo.GetMethods(BindingFlags.Instance | BindingFlags.Public).SingleOrDefault(method => method.Name == "IsEmptyOrUnknown" && method.GetParameters().Length == 0 && method.ReturnType == typeof(Boolean)); if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasEmptyValue)) { Assert.IsNotNull(emptyValue, "{0} should contain a static read-only Empty field.", svo); Assert.IsNotNull(isEmptyMethod, "{0} should contain a IsEmpty method.", svo); } else { Assert.IsNull(emptyValue, "{0} should not contain a static read-only Empty field.", svo); Assert.IsNull(isEmptyMethod, "{0} should not contain a IsEmpty method.", svo); } if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasUnknownValue)) { Assert.IsNotNull(unknownValue, "{0} should contain a static read-only Unknown field.", svo); Assert.IsNotNull(isUnknownMethod, "{0} should contain a IsUnknown method.", svo); } else { Assert.IsNull(unknownValue, "{0} should not contain a static read-only Unknown field.", svo); Assert.IsNull(isUnknownMethod, "{0} should not contain a IsUnknown method.", svo); } if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasEmptyValue) && attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasUnknownValue)) { Assert.IsNotNull(isEmptyOrUnknownMethod, "{0} should contain a IsEmptyOrUnknown method.", svo); } else { Assert.IsNull(isEmptyOrUnknownMethod, "{0} should not contain a IsEmptyOrUnknown method.", svo); } }
/// <summary>Returns true if the value should be represented by a <see cref="DBNull.Value"/>, otherwise false.</summary> private static bool IsDbNullValue(object value, Type sourceType, SingleValueObjectAttribute attr) { if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasEmptyValue)) { var defaultValue = Activator.CreateInstance(sourceType); return(Equals(value, defaultValue)); } return(false); }
public static void TryParseMatches(Type svo, SingleValueObjectAttribute attr) { Assert.IsNotNull(svo, nameof(svo)); Assert.IsNotNull(attr, nameof(attr)); var staticMethods = svo.GetMethods(BindingFlags.Public | BindingFlags.Static); // The out parameter. var byrefSvo = svo.MakeByRefType(); var tryParse = staticMethods.SingleOrDefault(method => method.Name == "TryParse" && method.GetParameters().Length == 2 && method.GetParameters()[0].ParameterType == typeof(string) && method.GetParameters()[1].ParameterType == byrefSvo && method.GetParameters()[1].IsOut && method.ReturnType == typeof(bool)); var tryParseCulture = staticMethods.SingleOrDefault(method => method.Name == "TryParse" && method.GetParameters().Length == 3 && method.GetParameters()[0].ParameterType == typeof(string) && method.GetParameters()[1].ParameterType == typeof(IFormatProvider) && method.GetParameters()[2].ParameterType == byrefSvo && method.GetParameters()[2].IsOut && method.ReturnType == typeof(bool)); if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.TryParse)) { Assert.IsNotNull(tryParse, $"{svo} should contain a static TryParse method."); if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.CultureDependent)) { Assert.IsNotNull(tryParseCulture, $"{svo} should contain a static TryParse method with an IFormatProvider parameter."); } else { Assert.IsNull(tryParseCulture, $"{svo} should not contain a static TryParse method with an IFormatProvider parameter."); } } else { Assert.IsNull(tryParse, $"{svo} should not contain a static TryParse method."); Assert.IsNull(tryParseCulture, $"{svo} should not contain a static TryParse method with an IFormatProvider parameter."); } }
public static void IsValidMatches(Type svo, SingleValueObjectAttribute attr) { Assert.IsNotNull(svo, nameof(svo)); Assert.IsNotNull(attr, nameof(attr)); var staticMethods = svo.GetMethods(BindingFlags.Public | BindingFlags.Static); var isValid = staticMethods.SingleOrDefault(method => method.Name == "IsValid" && method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(string) && method.ReturnType == typeof(bool)); var isValidCulture = staticMethods.SingleOrDefault(method => method.Name == "IsValid" && method.GetParameters().Length == 2 && method.GetParameters()[0].ParameterType == typeof(string) && method.GetParameters()[1].ParameterType == typeof(IFormatProvider) && method.ReturnType == typeof(bool)); if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.IsValid)) { Assert.IsNotNull(isValid, $"{svo} should contain a static IsValid method."); if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.CultureDependent)) { Assert.IsNotNull(isValidCulture, $"{svo} should contain a static IsValid method with an IFormatProvider parameter."); } else { Assert.IsNull(isValidCulture, $"{0} should not contain a static IsValid method with an IFormatProvider parameter."); } } else { Assert.IsNull(isValid, $"{svo} should not contain a static IsValid method."); Assert.IsNull(isValidCulture, $"{svo} should not contain a static IsValid method with an IFormatProvider parameter."); } }
public static void EmptyAndUnknownMatches(Type svo, SingleValueObjectAttribute attr) { var emptyValue = svo.GetFields(BindingFlags.Static | BindingFlags.Public).SingleOrDefault(field => field.Name == "Empty" && field.IsInitOnly && field.FieldType == svo); var unknownValue = svo.GetFields(BindingFlags.Static | BindingFlags.Public).SingleOrDefault(field => field.Name == "Unknown" && field.IsInitOnly && field.FieldType == svo); var minValue = svo.GetFields(BindingFlags.Static | BindingFlags.Public).SingleOrDefault(field => field.Name == "MinValue" && field.IsInitOnly && field.FieldType == svo); var maxValue = svo.GetFields(BindingFlags.Static | BindingFlags.Public).SingleOrDefault(field => field.Name == "MaxValue" && field.IsInitOnly && field.FieldType == svo); var isEmptyMethod = svo.GetMethods(BindingFlags.Instance | BindingFlags.Public).SingleOrDefault(method => method.Name == "IsEmpty" && method.GetParameters().Length == 0 && method.ReturnType == typeof(Boolean)); var isUnknownMethod = svo.GetMethods(BindingFlags.Instance | BindingFlags.Public).SingleOrDefault(method => method.Name == "IsUnknown" && method.GetParameters().Length == 0 && method.ReturnType == typeof(Boolean)); var isEmptyOrUnknownMethod = svo.GetMethods(BindingFlags.Instance | BindingFlags.Public).SingleOrDefault(method => method.Name == "IsEmptyOrUnknown" && method.GetParameters().Length == 0 && method.ReturnType == typeof(Boolean)); if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasEmptyValue)) { Assert.IsNotNull(emptyValue, "{0} should contain a static read-only Empty field.", svo); Assert.IsNotNull(isEmptyMethod, "{0} should contain a IsEmpty method.", svo); } else { Assert.IsNull(emptyValue, "{0} should not contain a static read-only Empty field.", svo); Assert.IsNull(isEmptyMethod, "{0} should not contain a IsEmpty method.", svo); } if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasUnknownValue)) { Assert.IsNotNull(unknownValue, "{0} should contain a static read-only Unknown field.", svo); Assert.IsNotNull(isUnknownMethod, "{0} should contain a IsUnknown method.", svo); } else { Assert.IsNull(unknownValue, "{0} should not contain a static read-only Unknown field.", svo); Assert.IsNull(isUnknownMethod, "{0} should not contain a IsUnknown method.", svo); } if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasEmptyValue) && attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasUnknownValue)) { Assert.IsNotNull(isEmptyOrUnknownMethod, "{0} should contain a IsEmptyOrUnknown method.", svo); } else { Assert.IsNull(isEmptyOrUnknownMethod, "{0} should not contain a IsEmptyOrUnknown method.", svo); } if (attr.StaticOptions == SingleValueStaticOptions.Continuous) { Assert.IsNotNull(minValue, "{0} should contain a static read-only Unknown MinValue.", svo); Assert.IsNotNull(maxValue, "{0} should contain a static read-only Unknown MaxValue.", svo); } }
public static void UnderlyingTypeMatches(Type svo, SingleValueObjectAttribute attr) { var m_Value = svo.GetField("m_Value", BindingFlags.Instance | BindingFlags.NonPublic); Assert.IsNotNull(m_Value, "{0} should contain a m_Value field.", svo); Assert.AreEqual(attr.UnderlyingType, m_Value.FieldType, "{0}.m_Field", svo); }
public static void TryParseMatches(Type svo, SingleValueObjectAttribute attr) { var staticMethods = svo.GetMethods(BindingFlags.Public | BindingFlags.Static); // The out parameter. var byrefSvo = svo.MakeByRefType(); var tryParse = staticMethods.SingleOrDefault(method => method.Name == "TryParse" && method.GetParameters().Length == 2 && method.GetParameters()[0].ParameterType == typeof(String) && method.GetParameters()[1].ParameterType == byrefSvo && method.GetParameters()[1].IsOut && method.ReturnType == typeof(Boolean)); var tryParseCulture = staticMethods.SingleOrDefault(method => method.Name == "TryParse" && method.GetParameters().Length == 3 && method.GetParameters()[0].ParameterType == typeof(String) && method.GetParameters()[1].ParameterType == typeof(IFormatProvider) && method.GetParameters()[2].ParameterType == byrefSvo && method.GetParameters()[2].IsOut && method.ReturnType == typeof(Boolean)); if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.TryParse)) { Assert.IsNotNull(tryParse, "{0} should contain a static TryParse method.", svo); if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.CultureDependent)) { Assert.IsNotNull(tryParseCulture, "{0} should contain a static TryParse method with an IFormatProvider parameter.", svo); } else { Assert.IsNull(tryParseCulture, "{0} should not contain a static TryParse method with an IFormatProvider parameter.", svo); } } else { Assert.IsNull(tryParse, "{0} should not contain a static TryParse method.", svo); Assert.IsNull(tryParseCulture, "{0} should not contain a static TryParse method with an IFormatProvider parameter.", svo); } }
public static void ParseMatches(Type svo, SingleValueObjectAttribute attr) { var staticMethods = svo.GetMethods(BindingFlags.Public | BindingFlags.Static); var parse = staticMethods.SingleOrDefault(method => method.Name == "Parse" && method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(String) && method.ReturnType == svo); var parseCulture = staticMethods.SingleOrDefault(method => method.Name == "Parse" && method.GetParameters().Length == 2 && method.GetParameters()[0].ParameterType == typeof(String) && method.GetParameters()[1].ParameterType == typeof(IFormatProvider) && method.ReturnType == svo); if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.Parse)) { Assert.IsNotNull(parse, "{0} should contain a static Parse method.", svo); if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.CultureDependent)) { Assert.IsNotNull(parseCulture, "{0} should contain a static Parse method with an IFormatProvider parameter.", svo); } else { Assert.IsNull(parseCulture, "{0} should not contain a static Parse method with an IFormatProvider parameter.", svo); } } else { Assert.IsNull(parse, "{0} should not contain a static Parse method.", svo); Assert.IsNull(parseCulture, "{0} should not contain a static Parse method an IFormatProvider parameter.", svo); } }
/// <summary>Returns true if the value should be represented by a <see cref="DBNull.Value"/>, otherwise false.</summary> private static bool IsDbNullValue(object value, Type sourceType, SingleValueObjectAttribute attr) { if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasEmptyValue)) { var defaultValue = Activator.CreateInstance(sourceType); return Equals(value, defaultValue); } return false; }