-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBase.cs
More file actions
124 lines (109 loc) · 3.86 KB
/
Copy pathBase.cs
File metadata and controls
124 lines (109 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Data;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Dynamic;
using System.Collections;
namespace xSkrape.APIWrapper
{
/// <summary>
/// Common extension methods used by xSkrape.
/// </summary>
public static class Extensions
{
/// <summary>
/// Renders DataTable contents in a printable string.
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string RenderDataTable(this DataTable dt)
{
int maxWidth = 20;
StringBuilder sb = new StringBuilder();
foreach (DataColumn dc in dt.Columns)
{
sb.Append(dc.ColumnName.LeftWithEllipsis(maxWidth) + " ");
}
sb.Append(Environment.NewLine);
foreach (DataRow dr in dt.Rows)
{
if (dr.RowState != DataRowState.Deleted)
{
foreach (DataColumn dc in dt.Columns)
{
sb.Append(dr[dc].ToString().LeftWithEllipsis(maxWidth) + " ");
}
sb.Append(Environment.NewLine);
}
}
return sb.ToString();
}
/// <summary>
/// Take n left characters of string with ellipsis used when truncated.
/// </summary>
/// <param name="s"></param>
/// <param name="length"></param>
/// <returns></returns>
public static string LeftWithEllipsis(this string s, int length)
{
if (string.IsNullOrEmpty(s))
{
return s;
}
if (s.Length > length)
{
return s.Substring(0, length - 2) + "..";
}
return s.PadRight(length);
}
/// <summary>
/// Allows for enumeration over a DataTable as an effective list of dynamic objects where columns can be accessed using .columnname
/// </summary>
/// <param name="dt">DataTable to transform</param>
/// <returns></returns>
public static IEnumerable<dynamic> AsDynamic(this DataTable dt)
{
foreach (DataRowView row in dt.DefaultView) yield return row.AsDynamic();
}
/// <summary>
/// Allows for enumeration over a DataView as an effective list of dynamic objects where columns can be accessed using .columnname
/// </summary>
/// <param name="dv">DataView to transform</param>
/// <returns></returns>
public static IEnumerable<dynamic> AsDynamic(this DataView dv)
{
foreach (DataRowView row in dv) yield return row.AsDynamic();
}
/// <summary>
/// Allows for access to columns of a DataRowView using .columnname notation
/// </summary>
/// <param name="row">DataRowView to access columns against</param>
/// <returns></returns>
public static dynamic AsDynamic(this DataRowView row)
{
return new DynamicDataRowView(row);
}
class DynamicDataRowView : DynamicObject
{
DataRowView _row;
public DynamicDataRowView(DataRowView row) { _row = row; }
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = _row[binder.Name];
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_row[binder.Name] = value;
return true;
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return _row.Row.Table.Columns.Cast<DataColumn>().Select(dc => dc.ColumnName);
}
}
}
}