コード例 #1
0
    static void Main()
    {
        var items = new IDataItem[] {
            new TestItem(),
            new TestItem(),
            new TestItem2(),
            new TestItem2(),
        };

        foreach (var kv in items.GroupBy(c => c.GetType()))
        {
            // group by actual type
            var type  = kv.Key;
            var batch = kv.ToArray();
            // grab BulkInsert<Type> method
            var insert = typeof(Test).GetMethod("BulkInsert").MakeGenericMethod(type);
            // create array of Type[]
            var casted = Array.CreateInstance(type, batch.Length);
            Array.Copy(batch, casted, batch.Length);
            // invoke
            insert.Invoke(new Test(), new object[] { casted });
        }

        Console.ReadKey();
    }