/
ConvertibleBase.cs
148 lines (130 loc) · 3.75 KB
/
ConvertibleBase.cs
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
namespace ArtOfNet.ConfArt.Core
{
public class DynamicConvertibleBase: DynamicObject, IConvertible
{
public virtual Func<string> GetValue { get; set; }
private string TryGetValue()
{
string result;
if (GetValue == null)
{
result = string.Empty; var x = new ExpandoObject();
}
else
{
try
{
result = GetValue();
}
catch
{
result = string.Empty;
}
}
return result;
}
#region IConvertible
public TypeCode GetTypeCode()
{
return TypeCode.String;
}
public bool ToBoolean(IFormatProvider provider)
{
bool outValue;
bool.TryParse(TryGetValue(), out outValue);
return outValue;
}
public byte ToByte(IFormatProvider provider)
{
byte outValue;
byte.TryParse(TryGetValue(), out outValue);
return outValue;
}
public char ToChar(IFormatProvider provider)
{
char outValue;
char.TryParse(TryGetValue(), out outValue);
return outValue;
}
public DateTime ToDateTime(IFormatProvider provider)
{
DateTime outValue;
DateTime.TryParse(TryGetValue(), out outValue);
return outValue;
}
public decimal ToDecimal(IFormatProvider provider)
{
decimal outValue;
decimal.TryParse(TryGetValue(), out outValue);
return outValue;
}
public double ToDouble(IFormatProvider provider)
{
double outValue;
double.TryParse(TryGetValue(), out outValue);
return outValue;
}
public short ToInt16(IFormatProvider provider)
{
Int16 outValue;
Int16.TryParse(TryGetValue(), out outValue);
return outValue;
}
public int ToInt32(IFormatProvider provider)
{
Int32 outValue;
Int32.TryParse(TryGetValue(), out outValue);
return outValue;
}
public long ToInt64(IFormatProvider provider)
{
Int64 outValue;
Int64.TryParse(TryGetValue(), out outValue);
return outValue;
}
public sbyte ToSByte(IFormatProvider provider)
{
sbyte outValue;
sbyte.TryParse(TryGetValue(), out outValue);
return outValue;
}
public float ToSingle(IFormatProvider provider)
{
float outValue;
float.TryParse(TryGetValue(), out outValue);
return outValue;
}
public string ToString(IFormatProvider provider)
{
return TryGetValue();
}
public object ToType(Type conversionType, IFormatProvider provider)
{
return (object)TryGetValue();
}
public ushort ToUInt16(IFormatProvider provider)
{
ushort outValue;
ushort.TryParse(TryGetValue(), out outValue);
return outValue;
}
public uint ToUInt32(IFormatProvider provider)
{
uint outValue;
uint.TryParse(TryGetValue(), out outValue);
return outValue;
}
public ulong ToUInt64(IFormatProvider provider)
{
ulong outValue;
ulong.TryParse(TryGetValue(), out outValue);
return outValue;
}
#endregion
}
}