Created
March 12, 2011 14:03
-
-
Save joshka/867254 to your computer and use it in GitHub Desktop.
A C# implementation of Stub for DataTable creation as a test helper.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Data; | |
namespace TestDataTableBuilder | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
DataTable dt = new StubDataTable("FirstName", "LastName", "Age") | |
{ | |
{ "John", "Doe", 25 }, | |
{ "Jane", "Doe", 24 } | |
}; | |
dt.TableName = "Person"; // can't write xml without a name for the table | |
dt.WriteXml(Console.Out); | |
} | |
} | |
class StubDataTable : IEnumerable | |
{ | |
private readonly DataTable _table; | |
public StubDataTable(params string[] columnNames) | |
{ | |
_table = new DataTable(); | |
foreach (var columnName in columnNames) | |
_table.Columns.Add(columnName); | |
} | |
public void Add(params object[] values) | |
{ | |
_table.LoadDataRow(values, LoadOption.OverwriteChanges); | |
} | |
public IEnumerator GetEnumerator() | |
{ | |
throw new NotImplementedException(); | |
} | |
public static implicit operator DataTable(StubDataTable dt) | |
{ | |
return dt._table; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment