using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Fasterflect; namespace LeafWeb.Core.Utility { public class ParseInfoPropertyMatcher where T : new() { public IList ParseInfoProperties { get; } public ParseInfoPropertyMatcher() { ParseInfoProperties = GetParseInfoProperties(); } private PropertyInfo[] GetParseInfoProperties() { var propertyInfos = typeof(T).Properties(); return propertyInfos.Where(p => AttributeExtensions.HasAttribute(p)).ToArray(); } public virtual PropertyInfo MatchProperty(string title, int position) { return ParseInfoProperties.FirstOrDefault(p => p.Attribute().IsTitleMatch(title)) ?? ParseInfoProperties.FirstOrDefault(p => p.Attribute().IsPositionMatch(position)); } public virtual PropertyInfo MatchPropertyExact(string title, int position) { return ParseInfoProperties .FirstOrDefault(p => { var attribute = p.Attribute(); return attribute.IsTitleMatch(title) && attribute.IsPositionMatch(position); }); } public virtual bool IsPropertiesTitlesMatch(string[] titles) { var propertyMatch = 0; var propertyNoMatch = 0; for (var i = 0; i < titles.Length; i++) { var title = titles[i]; var position = i + 1; if (String.IsNullOrEmpty(title)) continue; var property = MatchPropertyExact(title, position); if (property != null) propertyMatch++; else propertyNoMatch++; } return propertyMatch / (double)propertyNoMatch > .9; } } }