Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.13 KB | None | 0 0
  1. Friend Shared Function ConvertOrDefault(Of T As {Structure, IConvertible})(convertFrom As Object, ignoreCase As Boolean) As T
  2. Dim retVal As T
  3. If Not GetType(T).IsEnum Then
  4. Throw New ArgumentException("Type must be enum")
  5. ElseIf convertFrom Is Nothing OrElse Not TypeOf convertFrom Is String Then
  6. Return New T
  7. ElseIf [Enum].TryParse(convertFrom.ToString(), ignoreCase, retVal) Then
  8. Return retVal
  9. Else
  10. Return New T
  11. End If
  12. End Function
  13.  
  14. Friend Shared Function Convert(Of T)(value as Object) As T
  15. If GetType(T).IsEnum Then
  16. Return Enums.ConvertOrDefault(Of T)(value, True)
  17. Else : return DirectCast(value, T)
  18. End If
  19. End Function
  20.  
  21. Type argument 'T' does not inherit from or implement the constraint type 'System.IConvertible'
  22. Type argument 'T' does not satisfy the 'Structure' constraint for type parameter 'T'
  23.  
  24. Dim type As Type = GetType(T)
  25.  
  26. If type.IsEnum Then
  27. Select Case type.Name
  28. Case "EnumTypeOne"
  29. Return DirectCast(DirectCast(Enums.ConvertOrDefault(Of EnumTypeOne)(value, True), Object), T)
  30. ' ...
  31.  
  32. Dim id as Byte() = Convert(dataReader("id_column"))
  33. Dim something as SomeEnum = Convert(dataReader("somethingCol"))
  34. '...
  35.  
  36. Friend Shared Function ConvertOrDefault(Of T As{New}) convertFrom As Object, ignoreCase As Boolean) As T
  37. If Not GetType(T).IsEnum Then
  38. Throw New ArgumentException("Type must be enum")
  39. ElseIf convertFrom Is Nothing OrElse Not TypeOf convertFrom Is String Then
  40. Return New T
  41. End If
  42. Try
  43. Return CType([Enum].Parse(GetType(T), convertFrom.ToString(), ignoreCase), T)
  44. Catch ex As Exception
  45. End Try
  46.  
  47. ' default
  48. Return New T
  49. End Function
  50.  
  51. Friend Shared Function Convert(Of T)(value As Object) As T
  52. If GetType(T).IsEnum Then
  53. Return ConvertOrDefault(Of T)(value, True)
  54. Else
  55. Return DirectCast(value, T)
  56. End If
  57. End Function
  58.  
  59. Friend Shared Function ConvertOrDefault(Of TEnum)(convertFrom As Object, ignoreCase As Boolean) As TEnum
  60. ' Since this function only excepts Enum types, declaring the return value
  61. ' will initialize it to zero.
  62. Dim retVal As TEnum
  63. Dim typeTEnum As System.Type = GetType(TEnum)
  64. If typeTEnum.IsEnum Then
  65. Dim convertFromString As String = TryCast(convertFrom, String)
  66. If convertFrom IsNot Nothing AndAlso convertFromString IsNot Nothing Then
  67. Try
  68. retVal = DirectCast(System.Enum.Parse(typeTEnum, convertFromString), TEnum)
  69. Catch ex As ArgumentNullException
  70. ' eat it
  71. Catch ex As ArgumentException
  72. ' eat it
  73. Catch ex As OverflowException
  74. ' eat it
  75. End Try
  76. End If
  77. Else
  78. Throw New ArgumentException("Type must be enum")
  79. End If
  80. Return retVal
  81. End Function
  82.  
  83. public class BoolEnum
  84. {
  85. private static Dictionary<bool, BoolEnum> allValuesByNaturalValue = new Dictionary<bool, BoolEnum>();
  86. private static Dictionary<string, BoolEnum> allValuesByTextValue = new Dictionary<string, BoolEnum>();
  87. private static Dictionary<int, BoolEnum> allValuesByInteger = new Dictionary<int, BoolEnum>();
  88.  
  89. private string boolText;
  90. private int integerValue;
  91. private bool naturalValue;
  92.  
  93. public static readonly BoolEnum True = new BoolEnum(true, "Is True", 1);
  94. public static readonly BoolEnum False = new BoolEnum(false, "Is False", 0);
  95.  
  96. private BoolEnum(bool naturalValue, string boolText, int integerValue)
  97. {
  98. this.naturalValue = naturalValue;
  99. this.boolText = boolText;
  100. this.integerValue = integerValue;
  101. allValuesByNaturalValue.Add(naturalValue, this);
  102. allValuesByTextValue.Add(boolText, this);
  103. allValuesByInteger.Add(integerValue, this);
  104. }
  105.  
  106. public static BoolEnum TrySelect(bool naturalValue, BoolEnum defaultValue)
  107. {
  108. BoolEnum returnValue;
  109. if (allValuesByNaturalValue.TryGetValue(naturalValue, out returnValue)) return returnValue;
  110. return defaultValue;
  111. }
  112.  
  113. public static BoolEnum TrySelect(string boolText, BoolEnum defaultValue)
  114. {
  115. BoolEnum returnValue;
  116. if (allValuesByTextValue.TryGetValue(boolText, out returnValue)) return returnValue;
  117. return defaultValue;
  118. }
  119.  
  120. public static BoolEnum TrySelect(int integerValue, BoolEnum defaultValue)
  121. {
  122. BoolEnum returnValue;
  123. if (allValuesByInteger.TryGetValue(integerValue, out returnValue)) return returnValue;
  124. return defaultValue;
  125. }
  126.  
  127. public static implicit operator bool(BoolEnum boolEnum)
  128. {
  129. return boolEnum != null ? boolEnum.naturalValue : false;
  130. }
  131.  
  132. public static implicit operator string(BoolEnum boolEnum)
  133. {
  134. return boolEnum != null ? boolEnum.boolText : "Is False";
  135. }
  136.  
  137. public static implicit operator int(BoolEnum boolEnum)
  138. {
  139. return boolEnum != null ? boolEnum.integerValue : 0;
  140. }
  141.  
  142. public bool NaturalValue { get { return this.naturalValue; } }
  143. public string BoolText { get { return this.boolText; } }
  144. public int IntegerValue { get { return this.integerValue; } }
  145.  
  146. public static IReadOnlyCollection<BoolEnum> AllValues { get { return allValuesByNaturalValue.Values.ToList().AsReadOnly(); } }
  147. public static IReadOnlyCollection<bool> AllBooleanValues { get { return allValuesByNaturalValue.Keys.ToList().AsReadOnly(); } }
  148. public static IReadOnlyCollection<string> AllTextValues { get { return allValuesByTextValue.Keys.ToList().AsReadOnly(); } }
  149. public static IReadOnlyCollection<int> AllIntegerValues { get { return allValuesByInteger.Keys.ToList().AsReadOnly(); } }
  150.  
  151. public override string ToString()
  152. {
  153. return "[" + this.naturalValue.ToString() + ", "" + this.boolText.ToString() + "", " + this.integerValue.ToString() + "]";
  154. }
  155.  
  156. }
  157.  
  158. Public Class BoolEnum
  159. Private Shared allValuesByNaturalValue As New Dictionary(Of Boolean, BoolEnum)()
  160. Private Shared allValuesByTextValue As New Dictionary(Of String, BoolEnum)()
  161. Private Shared allValuesByInteger As New Dictionary(Of Integer, BoolEnum)()
  162.  
  163. Private m_boolText As String
  164. Private m_integerValue As Integer
  165. Private m_naturalValue As Boolean
  166.  
  167. Public Shared ReadOnly [True] As New BoolEnum(True, "Is True", 1)
  168. Public Shared ReadOnly [False] As New BoolEnum(False, "Is False", 0)
  169.  
  170. Private Sub New(naturalValue As Boolean, boolText As String, integerValue As Integer)
  171. Me.m_naturalValue = naturalValue
  172. Me.m_boolText = boolText
  173. Me.m_integerValue = integerValue
  174. allValuesByNaturalValue.Add(naturalValue, Me)
  175. allValuesByTextValue.Add(boolText, Me)
  176. allValuesByInteger.Add(integerValue, Me)
  177. End Sub
  178.  
  179. Public Shared Function TrySelect(naturalValue As Boolean, defaultValue As BoolEnum) As BoolEnum
  180. Dim returnValue As BoolEnum
  181. If allValuesByNaturalValue.TryGetValue(naturalValue, returnValue) Then
  182. Return returnValue
  183. End If
  184. Return defaultValue
  185. End Function
  186.  
  187. Public Shared Function TrySelect(boolText As String, defaultValue As BoolEnum) As BoolEnum
  188. Dim returnValue As BoolEnum
  189. If allValuesByTextValue.TryGetValue(boolText, returnValue) Then
  190. Return returnValue
  191. End If
  192. Return defaultValue
  193. End Function
  194.  
  195. Public Shared Function TrySelect(integerValue As Integer, defaultValue As BoolEnum) As BoolEnum
  196. Dim returnValue As BoolEnum
  197. If allValuesByInteger.TryGetValue(integerValue, returnValue) Then
  198. Return returnValue
  199. End If
  200. Return defaultValue
  201. End Function
  202.  
  203. Public Shared Widening Operator CType(boolEnum As BoolEnum) As Boolean
  204. Return If(boolEnum IsNot Nothing, boolEnum.naturalValue, False)
  205. End Operator
  206.  
  207. Public Shared Widening Operator CType(boolEnum As BoolEnum) As String
  208. Return If(boolEnum IsNot Nothing, boolEnum.boolText, "Is False")
  209. End Operator
  210.  
  211. Public Shared Widening Operator CType(boolEnum As BoolEnum) As Integer
  212. Return If(boolEnum IsNot Nothing, boolEnum.integerValue, 0)
  213. End Operator
  214.  
  215. Public ReadOnly Property NaturalValue() As Boolean
  216. Get
  217. Return Me.m_naturalValue
  218. End Get
  219. End Property
  220. Public ReadOnly Property BoolText() As String
  221. Get
  222. Return Me.m_boolText
  223. End Get
  224. End Property
  225. Public ReadOnly Property IntegerValue() As Integer
  226. Get
  227. Return Me.m_integerValue
  228. End Get
  229. End Property
  230.  
  231. Public Shared ReadOnly Property AllValues() As IReadOnlyCollection(Of BoolEnum)
  232. Get
  233. Return allValuesByNaturalValue.Values.ToList().AsReadOnly()
  234. End Get
  235. End Property
  236. Public Shared ReadOnly Property AllBooleanValues() As IReadOnlyCollection(Of Boolean)
  237. Get
  238. Return allValuesByNaturalValue.Keys.ToList().AsReadOnly()
  239. End Get
  240. End Property
  241. Public Shared ReadOnly Property AllTextValues() As IReadOnlyCollection(Of String)
  242. Get
  243. Return allValuesByTextValue.Keys.ToList().AsReadOnly()
  244. End Get
  245. End Property
  246. Public Shared ReadOnly Property AllIntegerValues() As IReadOnlyCollection(Of Integer)
  247. Get
  248. Return allValuesByInteger.Keys.ToList().AsReadOnly()
  249. End Get
  250. End Property
  251.  
  252. Public Overrides Function ToString() As String
  253. Return "[" + Me.m_naturalValue.ToString() + ", """ + Me.m_boolText.ToString() + """, " + Me.m_integerValue.ToString() + "]"
  254. End Function
  255.  
  256. End Class
  257.  
  258. Friend Function Convert(Of T)(convertFrom As Object, Optional ignoreCase As Boolean = True) As T
  259. If convertFrom Is Nothing Then Return Nothing
  260. If GetType(T) = GetType(DBNull) Then Return Nothing
  261. If GetType(T).IsEnum Then
  262. Return CTypeDynamic(Of T)([Enum].Parse(GetType(T), convertFrom.ToString(), ignoreCase))
  263. Else
  264. Return CTypeDynamic(Of T)(convertFrom)
  265. End If
  266. End Function
  267.  
  268. <Extension()>
  269. Public Function ToEnum(Of TEnum)(ByVal value As String, ByVal defaultValue As TEnum) As TEnum
  270. If String.IsNullOrEmpty(value) Then
  271. Return defaultValue
  272. End If
  273.  
  274. Return [Enum].Parse(GetType(TEnum), value, True)
  275. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement