Теперь я действительно застрял после двух дней поиска в Google и попыток всего.
У меня есть файл JSON с массивом записей. Все записи одинаковы, каждая содержит семь свойств (я сам создал JSON, поэтому у меня есть полный контроль над ним). Я хочу наследовать тип коллекции, чтобы добавить некоторые навороты к данным, полученным из JSON. Например, каждая запись (это описание таблицы перевода Брайля — не беспокойтесь, если вы не понимаете, о чем я говорю) имеет
Код: Выделить всё
LanguageКод: Выделить всё
var germanTables = new TableCollection()
.PopulateFromJson()
.FindByLanguage("de");
The Record
Код: Выделить всё
public readonly record struct TranslationTable(
string FileName,
string DisplayName,
string Language,
string TableType,
string ContractionType,
string Direction,
int DotsMode
) {
// Some helper methods
}
Код: Выделить всё
public class TableCollection: ICollection {
private const string TablesJson = @"LibLouis\tables.json";
private List tables = new List();
public int Count { get; }
public bool IsReadOnly { get; }
public TableCollection PopulateFromJson() {
using var file = File.OpenRead(TablesJson);
this.tables = JsonSerializer.Deserialize(file);
return this;
}
public TableCollection FindByLanguage(string Language) {
// NOTE! At this stage this.tables.Count returns 1, it finds a German table
this.tables = this.tables.FindAll((TranslationTable table) => table.Language == Language).ToList();
return this;
}
public void Add(TranslationTable item) => ((ICollection)tables).Add(item);
public void Clear() => ((ICollection)tables).Clear();
public bool Contains(TranslationTable item) => ((ICollection)tables).Contains(item);
public void CopyTo(TranslationTable[] array, int arrayIndex) => ((ICollection)tables).CopyTo(array, arrayIndex);
public bool Remove(TranslationTable item) => ((ICollection)tables).Remove(item);
public IEnumerator GetEnumerator() => ((IEnumerable)tables).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)tables).GetEnumerator();
}
Код: Выделить всё
FindAll()Код: Выделить всё
var tables = new TableCollection().PopulateFromJson().FindByLanguage("de");
// It's a Serilog-powered logger, it writes to file
Log.Debug(tables.Count.ToString());
Источник: https://stackoverflow.com/questions/781 ... -interface
Мобильная версия