public ObjectTableDataLoader(TableDescription description, ObjectDataTable <T> table) { if (description == null) { throw new ArgumentNullException(nameof(description)); } if (table == null) { throw new ArgumentNullException(nameof(table)); } this.description = description; this.table = table; formatter = new Lazy <Func <T, object[]> >(CreateFormatter); }
/// <summary> /// Returns the table specified by name. If a table with the specified name does not already exist, it will be created. /// </summary> /// <typeparam name="T">The type of entity that the table should contain.</typeparam> /// <param name="tableName"> /// Name of the table. /// <remarks> /// If this value is null then the name of the entity will be used. /// </remarks> /// </param> /// <returns>The existing table with the specified name, if it exists. Otherwise, a new table will be created.</returns> /// <exception cref="System.InvalidOperationException"> /// Thrown if the table exists, but the element type specified is incorrect. /// </exception> /// <example> /// <code language="c#"> /// public class Person /// { /// public string Name { get; set; } /// } /// ... /// var data = new ObjectData(); /// var table = data.Table<Person>(); /// table.Add(new Person { Name = "Fred" }); /// table.Add(new Person { Name = "Jeff" }); /// foreach (var person in data.Table<Person>()) /// { /// Debug.Print(person.Name); /// } /// // prints: /// // Fred /// // Jeff /// </code> /// </example> public ObjectDataTable <T> Table <T>(string tableName = null) { tableName = tableName ?? typeof(T).Name; IEnumerable table; if (!tables.TryGetValue(tableName, out table) || table == null) { table = new ObjectDataTable <T>(); tables[tableName] = table; } if (table is ObjectDataTable <T> ) { return((ObjectDataTable <T>)table); } throw new InvalidOperationException($"A table with the name '{tableName}' already exists, but the element type is incorrect.\r\nExpected type: '{typeof(T).Name}'\r\nActual type: '{table.GetType().GetGenericArguments()[0].Name}'"); }