using System.Collections.Immutable; using System.Reflection; using Yuanshen.Common.Data.Excel.Attributes; using Yuanshen.Common.Data.Provider; namespace Yuanshen.Common.Data.Excel; public sealed class ExcelTableCollection { private readonly ImmutableDictionary _tables; public ExcelTableCollection(IAssetProvider assetProvider) { ImmutableDictionary.Builder builder = ImmutableDictionary.CreateBuilder(); IEnumerable types = Assembly.GetExecutingAssembly().GetTypes() .Where(type => type.GetCustomAttribute() != null); foreach (Type type in types) { ExcelAttribute attribute = type.GetCustomAttribute()!; builder.Add(attribute.Type, new ExcelTable(assetProvider.GetExcelTableJson(attribute.AssetName), type)); } _tables = builder.ToImmutable(); } public ExcelTable GetTable(ExcelType type) => _tables[type]; public TExcel? GetExcel(ExcelType type, uint id) where TExcel : ExcelItem => _tables[type].GetById(id); }