Skip to content

Instantly share code, notes, and snippets.

@darrencauthon
Forked from khebbie/gist:721980
Created November 30, 2010 18:25
Show Gist options
  • Save darrencauthon/722125 to your computer and use it in GitHub Desktop.
Save darrencauthon/722125 to your computer and use it in GitHub Desktop.
//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(DynamicItems people)
{
var person = people.First();
string Id = Convert.ToInt32(person.Id);
string Name = person.Name;
// do your stuff
//...
}
//However I am not able to use DynamicItems as a parameter
//Hence I have to do the following
[Given(@"the following Dealer exists")]
public void GivenTheFollowingDealerExists(Table table)
{
var people = new DynamicItems(table);
string Id = Convert.ToInt32(people.FirstRow().Id);
string Name = people.FirstRow().Name;
// do your stuff
//...
}
public class DynamicItems : IEnumerable<dynamic>
{
private Table table;
public DynamicItems(Table table)
{
this.table = table;
}
public IEnumerator<dynamic> GetEnumerator()
{
foreach (var tableRow in table.Rows)
{
yield return new DynamicItemsRow(row);
}
yield break;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class DynamicItemsRow : DynamicObject
{
private readonly TableRow _specflowTableRow;
public DynamicItemsRow(TableRow specflowTableRow)
{
_specflowTableRow = specflowTableRow;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
string dynamicMethodName = binder.Name;
result = _specflowTableRow[dynamicMethodName];
return true;
}
}
@khebbie
Copy link

khebbie commented Nov 30, 2010

So is it possible to get the first method to work with specflow?
[Given(@"the following Dealer exists")]
public void GivenTheFollowingDealerExists(DynamicItems people)
{
var person = people.First();
string Id = Convert.ToInt32(person.Id);
string Name = person.Name;

// do your stuff
//...
}

@khebbie
Copy link

khebbie commented Dec 1, 2010

By the way IEnumerable is a bit of a problem I just found out:
http://blogs.msdn.com/b/cburrows/archive/2009/02/04/c-dynamic-part-vii.aspx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment