を解決するのに役立つ可能性があり、空のオブジェクトを返します。匿名オブジェクトのプロパティしたがって、作成されたデータリーダーには列がなく、DataTable.Loadメソッドはこのリーダーに対して空の行を作成することを拒否します。
可能な場合は具象クラスでそれを試してみてください。
class Thingy
{
public int Number { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Thingy> list = new List<Thingy>() { new Thingy { Number = 500 } };
DataTable table = new DataTable();
using (var reader = ObjectReader.Create(list))
{
table.Load(reader);
}
}
}
編集:実は、Fastmemberは、これらのプロパティにアクセスする完全に可能であるが、一般的なリスト(オブジェクト)の種類が見てからそれを防ぎますそれら。
//This creates a "strongly" typed list, instead of List<object>:
var list = new[] { (new { Number = 500 }) }.ToList();
DataTable table = new DataTable();
using (var reader = ObjectReader.Create(list))
{
table.Load(reader);
}
編集2:
List<object> list = new List<object> { new { Number = 500 } };
DataTable table = new DataTable();
// Note that type information is derived directly from the first object in the list,
// so try not to pass an empty one :)
using (var reader = new ObjectReader(list[0].GetType(), list, null))
{
table.Load(reader);
}
:コンストラクタを使用してFastmemberに型情報を渡すための別の方法がまだありますが、実際の実行時の型とのIEnumerableを提供できる場合にも動作するはずです
混合アイテムタイプのリストを作成することができるため、これは他のアプローチよりも危険です。 Fastmemberは、リスト内のすべてのアイテムが完全に同じタイプである必要があり、次のようなものは例外を発生させます:
//These two items are not of the same type. Look carefully at the "Extra" property:
List<object> list = new List<object> { new { Number = 500, Extra = true }, new { Number = 500, Extra = "Boom" } };