public static IOrderedEnumerable <string> PossiblePropertyNames(this PropertyInfo propertyInfo, IEnumerable <string> schemaNames = null) { var possibleNames = new Func <PropertyInfo, string[]>[] { p => new[] { Inflector.MakeInitialUpperCase(p.DeclaringType?.Name) + Inflector.MakeInitialUpperCase(p.Name) }, p => PrefixWithSchemaName(p.Name) .ToArray(), p => new[] { Inflector.MakeInitialUpperCase(p.Name) }, p => new[] { Inflector.CollapseNames(p.DeclaringType?.Name, p.Name) }, p => Inflector.StripLeftTerms(p.Name) .ToArray() }; // Important: The IOrderedEnumerable and OrderBy clause here preserves the naming precedence in possible names return(possibleNames.SelectMany(f => f(propertyInfo)).OrderBy(x => true)); IEnumerable <string> PrefixWithSchemaName(string name) { if (schemaNames == null) { yield break; } foreach (var schemaName in schemaNames) { yield return(Inflector.MakeInitialUpperCase(schemaName) + name); } } }
public void StripLeftTerms_test() { var result = Inflector.StripLeftTerms("AlphaBravoCharlieBravoCharlie") .ToArray(); Assert.AreEqual(4, result.Length); Assert.IsFalse(result.Contains("AlphaBravoCharlieBravoCharlie")); Assert.IsTrue(result.Contains("BravoCharlieBravoCharlie")); Assert.IsTrue(result.Contains("CharlieBravoCharlie")); Assert.IsTrue(result.Contains("BravoCharlie")); Assert.IsTrue(result.Contains("Charlie")); Assert.IsFalse(result.Contains(string.Empty)); }