Guest User

Untitled

a guest
Jul 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. //First the usage
  2. //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
  3. [Given(@"the following Dealer exists")]
  4. public void GivenTheFollowingDealerExists(DynamicSpecflowTable dynamicTable )
  5. {
  6.  
  7. string Id = Convert.ToInt32(dynamicTable.FirstRow().Id);
  8. string Name = dynamicTable.FirstRow().Name;
  9.  
  10. // do your stuff
  11. //...
  12. }
  13.  
  14. //However I am not able to use DynamicSpecflowTable as a parameter
  15. //Hence I have to do the following
  16. [Given(@"the following Dealer exists")]
  17. public void GivenTheFollowingDealerExists(Table table)
  18. {
  19. var dynamicTable = new DynamicSpecflowTable(table);
  20. string Id = Convert.ToInt32(dynamicTable.FirstRow().Id);
  21. string Name = dynamicTable.FirstRow().Name;
  22.  
  23. // do your stuff
  24. //...
  25. }
  26.  
  27.  
  28. public class DynamicSpecflowTable
  29. {
  30. private readonly List<dynamic> _tableRows;
  31. public DynamicSpecflowTable(Table table)
  32. {
  33. _tableRows = new List<dynamic>();
  34. foreach (var tableRow in table.Rows)
  35. {
  36. _tableRows.Add(new DynamicSpecflowTableRow(tableRow));
  37. }
  38. }
  39.  
  40. public dynamic this[int i]
  41. {
  42. get { return _tableRows[i]; }
  43. }
  44.  
  45. public dynamic FirstRow()
  46. {
  47. return _tableRows[0];
  48. }
  49.  
  50. public dynamic SecondRow()
  51. {
  52. return _tableRows[1];
  53. }
  54. }
  55.  
  56. public class DynamicSpecflowTableRow : DynamicObject
  57. {
  58. private readonly TableRow _specflowTableRow;
  59. public DynamicSpecflowTableRow(TableRow specflowTableRow)
  60. {
  61. _specflowTableRow = specflowTableRow;
  62. }
  63.  
  64. public override bool TryGetMember(GetMemberBinder binder, out object result)
  65. {
  66. string dynamicMethodName = binder.Name;
  67. result = _specflowTableRow[dynamicMethodName];
  68. return true;
  69. }
  70. }
Add Comment
Please, Sign In to add comment