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
|
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
namespace Test
{
static class EnumExtensions
{
/// <summary>
/// Prüft, ein bestimmter Enum-Wert in einer Instanz einer Enum vorhanden ist
/// </summary>
/// <param name="instance"></param>
/// <param name="flags">System.Enum, Wert der Enum, der geprüft werden soll</param>
/// <returns>System.Boolean</returns>
public static System.Boolean Obtain(this System.Enum instance, System.Enum flags)
{
System.Int32 ls = 0;
try
{
//prüfen
if (System.Enum.GetUnderlyingType(instance.GetType()) == typeof(uint))
{
ls = 10;
//Rückgabewert setzen
return (System.Convert.ToUInt32(instance) & System.Convert.ToUInt32(flags)) != 0;
}
else if (System.Enum.GetUnderlyingType(instance.GetType()) == typeof(int))
{
ls = 20;
//Rückgabewert setzen
return (System.Convert.ToInt32(instance) & System.Convert.ToInt32(flags)) != 0;
}
else if (System.Enum.GetUnderlyingType(instance.GetType()) == typeof(byte))
{
ls = 30;
//Rückgabewert setzen
return (System.Convert.ToByte(instance) & System.Convert.ToByte(flags)) != 0;
}
else if (System.Enum.GetUnderlyingType(instance.GetType()) == typeof(sbyte))
{
ls = 40;
//Rückgabewert setzen
return (System.Convert.ToSByte(instance) & System.Convert.ToSByte(flags)) != 0;
}
else if (System.Enum.GetUnderlyingType(instance.GetType()) == typeof(short))
{
ls = 50;
//Rückgabewert setzen
return (System.Convert.ToInt16(instance) & System.Convert.ToInt16(flags)) != 0;
}
else if (System.Enum.GetUnderlyingType(instance.GetType()) == typeof(ushort))
{
ls = 60;
//Rückgabewert setzen
return (System.Convert.ToUInt16(instance) & System.Convert.ToUInt16(flags)) != 0;
}
else if (System.Enum.GetUnderlyingType(instance.GetType()) == typeof(long))
{
ls = 70;
//Rückgabewert setzen
return (System.Convert.ToInt64(instance) & System.Convert.ToInt64(flags)) != 0;
}
else if (System.Enum.GetUnderlyingType(instance.GetType()) == typeof(ulong))
{
ls = 80;
//Rückgabewert setzen
return (System.Convert.ToUInt64(instance) & System.Convert.ToUInt64(flags)) != 0;
}
//Rückgabewert setzen
return false;
}
catch (System.Exception ex)
{
//Fehler anzeigen
System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString() + " - System.Boolean Obtain(System.Enum, System.Enum) - " + ls.ToString() + " - " + ex.Message.ToString() + " - " + ex.StackTrace.ToString());
//Rückgabewert setzen
return false;
}
}
/// <summary>
/// Erstellt ein Array von System.Enum, das nur die Werte der Instanz enthält
/// </summary>
/// <param name="instance"></param>
/// <returns>System.Enum[]</returns>
public static System.Enum[] ContainedValues(this System.Enum instance)
{
System.Int32 ls = 0;
try
{
//Type ermitteln
System.Type et = instance.GetType();
//Array von FieldInfo erstellen
System.Reflection.FieldInfo[] fi = et.GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
//Array von System.Enum erstellen => Rückgabewert
//System.Enum[] values = new System.Enum[fi.Length];
System.Collections.Generic.List<System.Enum> values = new List<System.Enum>();
//alle Felder von instance durchlaufen
for (int iEnum = 0; iEnum < fi.Length; iEnum++)
{
//prüfen, ob der Wert vorhanden ist
if (instance.Obtain((System.Enum)fi[iEnum].GetValue(instance)) == true)
{
//values[index] = (System.Enum)fi[iEnum].GetValue(instance);
values.Add((System.Enum)fi[iEnum].GetValue(instance));
}
}
//values.Count prüfen
if (values.Count == 0)
{
//0-Wert setzen
values.Add((System.Enum)System.Enum.Parse(et, "0", true));
}
//Rückgabewert setzen
return values.ToArray();
}
catch (System.Exception ex)
{
//Fehler anzeigen
System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString() + " - System.Enum[] ContainedValues(System.Enum) - " + ls.ToString() + " - " + ex.Message.ToString() + " - " + ex.StackTrace.ToString());
//Rückgabewert erstellen
System.Enum[] retValue = new System.Enum[0];
retValue[0] = (System.Enum)System.Enum.Parse(instance.GetType(), "0", true);
//Rückgabewert setzen
return retValue;
}
}
}
}
|