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;
}
}
@darrencauthon
Copy link
Author

These are just my first thoughts...

Instead of a dynamic table, I changed the name to DynamicItems which implements IEnumerable. I did this only because if someone was to create properties like .Id, .Name, etc., those properties would be meant to be tied to some "thing," not a row. Then it makes it look like you're actually dealing with a set of items, not a table.

I like your idea, but I wish there was some way to get rid of the Convert.ToInt32() type of stuff. It's not a problem with your idea, it's just the language I guess. :)

@khebbie
Copy link

khebbie commented Nov 30, 2010

Yeah like this idea, however my original thought was to make DynamicTable inherit from table and be used as a parameter for steps where Table is used today.
That way when you make tables in your features you don't need a suitable object, but can use the dynamic keyword...

@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