-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathWrapper.cs
More file actions
198 lines (184 loc) · 9.12 KB
/
Copy pathWrapper.cs
File metadata and controls
198 lines (184 loc) · 9.12 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
namespace Cuemon.Extensions
{
/// <summary>
/// Provides helper method for a <see cref="Wrapper{T}"/> object.
/// </summary>
public static class Wrapper
{
/// <summary>
/// Parses the encapsulated instance of the specified <paramref name="wrapper"/> for a human-readable string value.
/// </summary>
/// <typeparam name="T">The type of the encapsulated instance of <paramref name="wrapper"/>.</typeparam>
/// <param name="wrapper">The wrapper object to parse the instance.</param>
/// <returns>A human-readable <see cref="string"/> representation of the wrapped instance in the <see cref="Wrapper{T}"/> object.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="wrapper"/> is null.
/// </exception>
public static string ParseInstance<T>(IWrapper<T> wrapper)
{
Validator.ThrowIfNull(wrapper);
switch (Type.GetTypeCode(wrapper.InstanceType))
{
case TypeCode.Boolean:
return wrapper.Instance.ToString().ToLowerInvariant();
case TypeCode.Byte:
case TypeCode.Decimal:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.SByte:
case TypeCode.Single:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Double:
return wrapper.InstanceAs<IConvertible>().ToString(CultureInfo.InvariantCulture);
case TypeCode.DateTime:
return wrapper.InstanceAs<DateTime>().ToString("O", CultureInfo.InvariantCulture);
case TypeCode.String:
return wrapper.Instance.ToString();
default:
if (Decorator.Enclose(wrapper.InstanceType).HasKeyValuePairImplementation())
{
var keyProperty = wrapper.InstanceType.GetProperty("Key");
var valueProperty = wrapper.InstanceType.GetProperty("Value");
var keyValue = keyProperty.GetValue(wrapper.Instance, null) ?? "null";
var valueValue = valueProperty.GetValue(wrapper.Instance, null) ?? "null";
return string.Format(CultureInfo.InvariantCulture, "[{0},{1}]", keyValue, valueValue);
}
if (Decorator.Enclose(wrapper.InstanceType).HasComparerImplementation() || Decorator.Enclose(wrapper.InstanceType).HasEqualityComparerImplementation())
{
return Decorator.Enclose(wrapper.InstanceType).ToFriendlyName();
}
switch (wrapper.InstanceType.Name.ToUpperInvariant())
{
case "BYTE[]":
return Convert.ToBase64String(wrapper.InstanceAs<byte[]>());
case "GUID":
return wrapper.InstanceAs<Guid>(CultureInfo.InvariantCulture).ToString("D");
case "RUNTIMETYPE":
return Decorator.Enclose(wrapper.InstanceAs<Type>()).ToFriendlyName();
case "URI":
return wrapper.InstanceAs<Uri>().OriginalString;
default:
return wrapper.Instance.ToString();
}
}
}
}
/// <summary>
/// Provides a way to wrap an object of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of the object to wrap.</typeparam>
public class Wrapper<T> : IWrapper<T>
{
private T _instance;
private Type _instanceType;
private MemberInfo _memberReference;
/// <summary>
/// Initializes a new instance of the <see cref="Wrapper{T}"/> class.
/// </summary>
protected Wrapper()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Wrapper{T}"/> class.
/// </summary>
/// <param name="instance">The instance that this wrapper object represents.</param>
/// <param name="memberReference">The member from where <paramref name="instance"/> was referenced.</param>
public Wrapper(T instance, MemberInfo memberReference = null)
{
Validator.ThrowIfNull(instance);
_instance = instance;
_instanceType = instance.GetType();
_memberReference = memberReference;
}
/// <summary>
/// Gets the object that this wrapper represents.
/// </summary>
/// <value>The object that this wrapper represents.</value>
public virtual T Instance
{
get => _instance;
protected set => _instance = value;
}
/// <summary>
/// Gets the type of the object that this wrapper represents.
/// </summary>
/// <value>The type of the that this wrapper represents.</value>
public virtual Type InstanceType
{
get => _instanceType;
protected set => _instanceType = value;
}
/// <summary>
/// Gets the member from where <see cref="Instance"/> was referenced.
/// </summary>
/// <value>The member from where <see cref="Instance"/> was referenced.</value>
public virtual MemberInfo MemberReference
{
get => _memberReference;
protected set => _memberReference = value;
}
/// <summary>
/// Gets a value indicating whether this instance has a member reference.
/// </summary>
/// <value><c>true</c> if this instance has a member reference; otherwise, <c>false</c>.</value>
public virtual bool HasMemberReference => (_memberReference != null);
/// <summary>
/// Gets a collection of key/value pairs that provide additional user-defined information about this wrapper object.
/// </summary>
/// <value>An object that implements the <see cref="IDictionary{TKey,TValue}"/> interface and contains a collection of user-defined key/value pairs.</value>
public virtual IDictionary<string, object> Data { get; } = new Dictionary<string, object>();
/// <summary>
/// Returns a value that is equivalent to the instance of the object that this wrapper represents.
/// </summary>
/// <typeparam name="TResult">The type of the return value.</typeparam>
/// <returns>A value that is equivalent to the instance of the object that this wrapper represents.</returns>
/// <exception cref="InvalidCastException">
/// The conversion is not supported - or - <see cref="Instance"/> does not implement the <see cref="IConvertible"/> interface.
/// </exception>
/// <exception cref="FormatException">
/// <see cref="Instance"/> is not in a format for <typeparamref name="T"/> recognized by <see cref="CultureInfo.InvariantCulture"/>.
/// </exception>
/// <exception cref="OverflowException">
/// <see cref="Instance"/> represents a number that is out of the range of <typeparamref name="T"/>.
/// </exception>
public TResult InstanceAs<TResult>()
{
return InstanceAs<TResult>(CultureInfo.InvariantCulture);
}
/// <summary>
/// Returns a value that is equivalent to the instance of the object that this wrapper represents.
/// </summary>
/// <typeparam name="TResult">The type of the return value.</typeparam>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>A value that is equivalent to the instance of the object that this wrapper represents.</returns>
/// <exception cref="InvalidCastException">
/// The conversion is not supported - or - <see cref="Instance"/> does not implement the <see cref="IConvertible"/> interface.
/// </exception>
/// <exception cref="FormatException">
/// <see cref="Instance"/> is not in a format for <typeparamref name="T"/> recognized by <paramref name="provider"/>.
/// </exception>
/// <exception cref="OverflowException">
/// <see cref="Instance"/> represents a number that is out of the range of <typeparamref name="T"/>.
/// </exception>
public TResult InstanceAs<TResult>(IFormatProvider provider)
{
var presult = Decorator.Enclose<object>(Instance).ChangeType(InstanceType, o => o.FormatProvider = provider);
return Decorator.Enclose(presult).ChangeTypeOrDefault<TResult>();
}
/// <summary>
/// Returns a <see cref="string" /> that represents this instance.
/// </summary>
/// <returns>A <see cref="string" /> that represents this instance.</returns>
public override string ToString()
{
return Wrapper.ParseInstance(this);
}
}
}