private static PropertyInfo GetThePropertyOnThisObject(object @object, string propertyName) { var type = @object.GetType(); return(type.GetProperties() .FirstOrDefault(x => TEHelpers.IsPropertyMatchingToColumnName(x, propertyName))); }
/// <summary> /// Indicates whether the table is equivalent to the specified instance by comparing the values of all /// columns against the properties of the instance. Will return false after finding the first difference. /// </summary> public static bool IsEquivalentToInstance <T>(this Table table, T instance) { AssertThatTheInstanceExists(instance); var instanceTable = TEHelpers.GetTheProperInstanceTable(table, typeof(T)); return(HasDifference(instanceTable, instance) == false); }
public static T CreateInstance <T>(this Table table, InstanceCreationOptions creationOptions) { var instanceTable = TEHelpers.GetTheProperInstanceTable(table, typeof(T)); return(TEHelpers.ThisTypeHasADefaultConstructor <T>() ? TEHelpers.CreateTheInstanceWithTheDefaultConstructor <T>(instanceTable, creationOptions) : TEHelpers.CreateTheInstanceWithTheValuesFromTheTable <T>(instanceTable, creationOptions)); }
public static void CompareToInstance <T>(this Table table, T instance) { AssertThatTheInstanceExists(instance); var instanceTable = TEHelpers.GetTheProperInstanceTable(table, typeof(T)); var differences = FindAnyDifferences(instanceTable, instance); if (ThereAreAnyDifferences(differences)) { ThrowAnExceptionThatDescribesThoseDifferences(differences); } }
public static T FindInSet <T>(this Table table, IEnumerable <T> set) { var instanceTable = TEHelpers.GetTheProperInstanceTable(table, typeof(T)); var matches = set.Where(instance => InstanceMatchesTable(instance, instanceTable)).ToArray(); if (matches.Length > 1) { throw new ComparisonException("Multiple instances match the table"); } return(matches.FirstOrDefault()); }
private void AssertThatAllColumnsInTheTableMatchToPropertiesOnTheType() { var normalizedPropertyNames = new HashSet <string>(from property in typeof(T).GetProperties() select TEHelpers.NormalizePropertyNameToMatchAgainstAColumnName(property.Name)); var normalizedColumnNames = new HashSet <string>(from columnHeader in table.Header select TEHelpers.NormalizePropertyNameToMatchAgainstAColumnName(TEHelpers.RemoveAllCharactersThatAreNotValidInAPropertyName(columnHeader))); var propertiesThatDoNotExist = normalizedColumnNames.Except(normalizedPropertyNames, StringComparer.OrdinalIgnoreCase).ToArray(); if (propertiesThatDoNotExist.Any()) { throw new ComparisonException($@"The following fields do not exist:{Environment.NewLine}{string.Join(Environment.NewLine, propertiesThatDoNotExist)}"); } }
private static bool ThePropertyDoesNotExist <T>(T instance, TableRow row) { return(instance.GetType().GetProperties() .Any(property => TEHelpers.IsMemberMatchingToColumnName(property, row.Id())) == false); }
public static void FillInstance(this Table table, object instance, InstanceCreationOptions creationOptions) { var instanceTable = TEHelpers.GetTheProperInstanceTable(table, instance.GetType()); TEHelpers.LoadInstanceWithKeyValuePairs(instanceTable, instance, creationOptions); }
private void AssertThatAllColumnsInTheTableMatchToPropertiesOnTheType() { var propertiesThatDoNotExist = from columnHeader in table.Header where (typeof(T).GetProperties().Any(property => TEHelpers.IsPropertyMatchingToColumnName(property, columnHeader)) == false) select columnHeader; if (propertiesThatDoNotExist.Any()) { throw new ComparisonException( propertiesThatDoNotExist.Aggregate(@"The following fields do not exist:", (running, next) => running + string.Format("{0}{1}", Environment.NewLine, next))); } }
public static IEnumerable <T> FindAllInSet <T>(this Table table, IEnumerable <T> set) { var instanceTable = TEHelpers.GetTheProperInstanceTable(table, typeof(T)); return(set.Where(instance => InstanceMatchesTable(instance, instanceTable)).ToArray()); }
public static void FillInstance <T>(this Table table, T instance) { var instanceTable = TEHelpers.GetTheProperInstanceTable <T>(table); TEHelpers.LoadInstanceWithKeyValuePairs(instanceTable, instance); }
public static T CreateInstance <T>(this Table table) { return(TEHelpers.ThisTypeHasADefaultConstructor <T>() ? TEHelpers.CreateTheInstanceWithTheDefaultConstructor <T>(table) : TEHelpers.CreateTheInstanceWithTheValuesFromTheTable <T>(table)); }
public static void FillInstance <T>(this Table table, T instance) { TEHelpers.LoadInstanceWithKeyValuePairs(table, instance); }