private bool _TryRelation(out object instance) { instance = null; if (!_Field.FieldType.GetInterfaces().Where(i => i == typeof(IRelatable)).Any()) { return(false); } var rows = from row in _Finder.FindRelatables(_Field.FieldType) select row; if (!rows.Any()) { return(false); } var colValue = (from col in _Row.GetColumns() where col.Name == _Field.Name select col.Value).FirstOrDefault(); if (colValue == null) { return(false); } var relatableRows = from relatable in rows where relatable.Compare(colValue) select relatable; try { instance = relatableRows.Single(); return(true); } catch (System.InvalidOperationException ioe) { throw new Exception($"No related type was found. Type:{_Field.DeclaringType.FullName} Related Type:{_Field.FieldType.FullName} Key:{colValue}", ioe); } }
public override object Parse(FieldInfo field, IEnumerable <Column> row, ITableable table) { var col = row.FirstOrDefault(c => c.Name == field.Name); if (col.Value == null) { return(null); } if (!field.FieldType.HasElementType) { return(false); } var elementType = field.FieldType.GetElementType(); var relatables = table.FindRelatables(elementType).Where(r => r.Compare(col.Value)).ToArray(); var length = relatables.Length; var val = Array.CreateInstance(elementType, length) as System.Array; for (int i = 0; i < length; i++) { val.SetValue(relatables[i], i); } return(val); }