-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUniversalEnumListSolutionWithXceed.cs
251 lines (217 loc) · 8.52 KB
/
UniversalEnumListSolutionWithXceed.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Data;
using System.Windows.Markup;
namespace WPF_dnscrypt_proxy_md
{
/* I was feckly waiting on for half an hour for git pulling of Xceed.Wpf.Toolkit from github(depth=1)
* I attached it and converted it to netcoreapp3.1 wpf profile in less than 5 minutes
* I have to figure out the internal mystery of the Selector class
* I have to modify it since I had named this frankly new cs file 'UniversalEnumListSolutionWithXceed' in the first place
* Now feel free to copy all around to your business working codes, I just did it.
* Happy ending
*/
[Flags]
public enum ServerFlag
{
DNSSEC = 1,
[DisplayString("No Log")]
NoLog = 1 << 1,
[DisplayString("No Filter")]
NoFilter = 1 << 2,
}
public enum Protocol
{
[DisplayString("Plain(reserved)")]
Plain = 0, //reserved for future
DNSCrypt,
DoH,
[DisplayString("TLS(reserved)")]
TLS, //reserved for future
DOTEx = 0x45,
DOHEx = 0x69,
DNSCryptRelay = 0x81,
Proxy = 0x96,
DNSCryptEx = 0xA9,
}
public enum SNIBlotUpType
{
[DisplayString("Default No Change")]
SNIBlotUpTypeDefault = 0,
[DisplayString("Omit SNI")] //SPECIFY&VERIFY SNIShadow
SNIBlotUpTypeOmit,
[DisplayString("SNI=Host IP Address")] //SPECIFY&VERIFY SNIShadow
SNIBlotUpTypeIPAddr,
[DisplayString("SNI=SNIShadow")]
SNIBlotUpTypeMoniker,
}
public class EnumConverter : MarkupExtension, IValueConverter
{
public EnumConverter() {}
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (parameter is Type && null != value && value.ToString() != string.Empty)
{
var enumValue = (Enum)Enum.Parse((Type)parameter, value.ToString());
string getDisplayText(string itemString, Type t){
var field = t.GetField(itemString);
if (null == field) return null; //out scope of mapping
var attribs = field.GetCustomAttributes(typeof(DisplayStringAttribute), false);
return null != attribs && attribs.Length > 0 ? ((DisplayStringAttribute)attribs[0]).Value : itemString;
}
var list = (from str in enumValue.ToString().Split(',')
let itemString = str.Trim()
let t= (Type)parameter
let text = getDisplayText(itemString, t)
select new KeyValuePair<long?, string>(
System.Convert.ToInt64(Enum.Parse((Type)parameter, itemString)), text)).ToList();
return list;
}
else
{
return new List<KeyValuePair<long?, string>>();
}
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
Int64? returnValue = null;
if (null == value)
return null;
if (parameter is Type t)
{
var list = value as IList<KeyValuePair<long?, string>>;
if (null != t.GetCustomAttribute<FlagsAttribute>())
returnValue = (from item in list select item.Key).DefaultIfEmpty(null).Aggregate((a, b) => a | b);
else
returnValue = list.Count > 1 ? (Int64?)null : (from item in list select item.Key).SingleOrDefault();
}
return returnValue;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
[AttributeUsage(AttributeTargets.Field)]
public sealed class DisplayStringAttribute : Attribute
{
private readonly string value;
public string Value
{
get { return value; }
}
public string ResourceKey { get; set; }
public DisplayStringAttribute(string v)
{
this.value = v;
}
public DisplayStringAttribute()
{
}
}
/// <summary>
/// Markup extension that provides a list of the members of a given enum.
/// </summary>
public class EnumListExtension : MarkupExtension
{
#region Member Variables
private Type _enumType;
private bool _asString;
#endregion //Member Variables
#region Constructor
/// <summary>
/// Initializes a new <see cref=”EnumListExtension”/>
/// </summary>
public EnumListExtension()
{
}
/// <summary>
/// Initializes a new <see cref=”EnumListExtension”/>
/// </summary>
/// <param name=”enumType”>The type of enum whose members are to be returned.</param>
public EnumListExtension(Type enumType)
{
this.EnumType = enumType;
}
#endregion //Constructor
#region Properties
/// <summary>
/// Gets/sets the type of enumeration to return
/// </summary>
public Type EnumType
{
get { return this._enumType; }
set
{
if (value != this._enumType)
{
if (null != value)
{
Type enumType = Nullable.GetUnderlyingType(value) ?? value;
if (enumType.IsEnum == false)
throw new ArgumentException("Type must be for an Enum.");
}
this._enumType = value;
}
}
}
public bool DBNull { get; set; }
/// <summary>
/// Gets/sets a value indicating whether to display the enumeration members as strings using the Description on the member if available.
/// </summary>
public bool AsString
{
get { return this._asString; }
set { this._asString = value; }
}
#endregion //Properties
#region Base class overrides
/// <summary>
/// Returns a list of items for the specified <see cref=”EnumType”/>. Depending on the <see cref=”AsString”/> property, the
/// items will be returned as the enum member value or as strings.
/// </summary>
/// <param name=”serviceProvider”>An object that provides services for the markup extension.</param>
/// <returns></returns>
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (null == this._enumType)
throw new InvalidOperationException("The EnumType must be specified.");
Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
Array enumValues = Enum.GetValues(actualEnumType);
// if the object itself is to be returned then just use GetValues
//
if (this._asString == false)
{
if (actualEnumType == this._enumType)
return enumValues;
Array tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1);
enumValues.CopyTo(tempArray, 1);
return tempArray;
}
var items = new Dictionary<long?, string>();
if (actualEnumType != this._enumType)
items.Add(null, string.Empty);
// otherwise we must process the list
foreach (object item in (from object v in Enum.GetValues(this._enumType) orderby (int)v select v))
{
string itemString = item.ToString();
FieldInfo field = this._enumType.GetField(itemString);
object[] attribs = field.GetCustomAttributes(typeof(DisplayStringAttribute), false);
if (null != attribs && attribs.Length > 0)
itemString = ((DisplayStringAttribute)attribs[0]).Value;
if (item.ToString().ToLower() == "none" && DBNull)
{
items.Add(null, string.Empty);
continue;
}
items.Add(Convert.ToInt64(item), itemString);
}
return items;
}
#endregion //Base class overrides
}
}