Skip to content

Instantly share code, notes, and snippets.

@khebbie
Created November 30, 2010 17:01
Show Gist options
  • Save khebbie/721980 to your computer and use it in GitHub Desktop.
Save khebbie/721980 to your computer and use it in GitHub Desktop.
First try at doing DynamicTable
//First the usage
//Would be cool to use it for taking out parameters from a table, when I only use a few parameters and don't have a real class
[Given(@"the following Dealer exists")]
public void GivenTheFollowingDealerExists(DynamicSpecflowTable dynamicTable )
{
string Id = Convert.ToInt32(dynamicTable.FirstRow().Id);
string Name = dynamicTable.FirstRow().Name;
// do your stuff
//...
}
//However I am not able to use DynamicSpecflowTable as a parameter
//Hence I have to do the following
[Given(@"the following Dealer exists")]
public void GivenTheFollowingDealerExists(Table table)
{
var dynamicTable = new DynamicSpecflowTable(table);
string Id = Convert.ToInt32(dynamicTable.FirstRow().Id);
string Name = dynamicTable.FirstRow().Name;
// do your stuff
//...
}
public class DynamicSpecflowTable
{
private readonly List<dynamic> _tableRows;
public DynamicSpecflowTable(Table table)
{
_tableRows = new List<dynamic>();
foreach (var tableRow in table.Rows)
{
_tableRows.Add(new DynamicSpecflowTableRow(tableRow));
}
}
public dynamic this[int i]
{
get { return _tableRows[i]; }
}
public dynamic FirstRow()
{
return _tableRows[0];
}
public dynamic SecondRow()
{
return _tableRows[1];
}
}
public class DynamicSpecflowTableRow : DynamicObject
{
private readonly TableRow _specflowTableRow;
public DynamicSpecflowTableRow(TableRow specflowTableRow)
{
_specflowTableRow = specflowTableRow;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
string dynamicMethodName = binder.Name;
result = _specflowTableRow[dynamicMethodName];
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment