1717#region U S A G E S
1818
1919using System ;
20- using System . Text . RegularExpressions ;
21- using DomainCommonExtensions . Resources ;
20+ using DomainCommonExtensions . CommonExtensions ;
2221
2322#endregion
2423
2524namespace DomainCommonExtensions . DataTypeExtensions
2625{
26+ /// -------------------------------------------------------------------------------------------------
2727 /// <summary>
28- /// Guid extensions
28+ /// Guid extensions.
2929 /// </summary>
30- /// <remarks></remarks>
30+ /// =================================================================================================
3131 public static class GuidExtensions
3232 {
33+ /// -------------------------------------------------------------------------------------------------
3334 /// <summary>
34- /// Check if is string is value
35+ /// Parse string to Guid.
3536 /// </summary>
36- /// <param name="source"></param>
37- /// <returns></returns>
38- public static bool IsGuid ( this string source )
39- {
40- if ( source . IsNullOrEmpty ( ) ) return false ;
41-
42- var options = RegexOptions . IgnoreCase | RegexOptions . Multiline ;
43- var guidRegEx = new Regex ( RegularExpressions . GUID , options ) ;
44-
45- return guidRegEx . IsMatch ( source ) ;
46- }
47-
48- /// <summary>
49- /// Parse string to Guid
50- /// </summary>
51- /// <param name="source"></param>
52- /// <returns></returns>
53- public static Guid ToGuid ( this string source )
54- {
55- if ( source . IsNullOrEmpty ( ) ) return Guid . Empty ;
56- try
57- {
58- return Guid . Parse ( source ) ;
59- }
60- catch
61- {
62- return Guid . Empty ;
63- }
64- }
65-
66- /// <summary>
67- /// Parse string to Guid from format: "\"9ffd2c3-6er456ds\""
68- /// </summary>
69- /// <param name="source"></param>
70- /// <returns></returns>
71- public static Guid FromDoubleQuotesWithBackSlashesToGuid ( this string source )
72- {
73- if ( source . IsNullOrEmpty ( ) ) return Guid . Empty ;
74- try
75- {
76- return Guid . ParseExact ( source . Replace ( "-" , "" ) . Replace ( "\" " , "" ) , "N" ) ;
77- }
78- catch
79- {
80- return Guid . Empty ;
81- }
82- }
83-
84- /// <summary>
85- /// Parse string to Guid
86- /// </summary>
87- /// <param name="source"></param>
88- /// <returns></returns>
37+ /// <param name="source">.</param>
38+ /// <returns>
39+ /// A Guid?
40+ /// </returns>
41+ /// =================================================================================================
8942 public static Guid ? TryToGuid ( this string source )
9043 {
9144 if ( source . IsNullOrEmpty ( ) ) return null ;
@@ -99,28 +52,58 @@ public static Guid FromDoubleQuotesWithBackSlashesToGuid(this string source)
9952 }
10053 }
10154
55+ /// -------------------------------------------------------------------------------------------------
10256 /// <summary>
103- /// Guid to int 32
57+ /// Guid to int 32.
10458 /// </summary>
105- /// <param name="uuid"></param>
106- /// <returns></returns>
59+ /// <param name="uuid">.</param>
60+ /// <returns>
61+ /// Uuid as an int.
62+ /// </returns>
63+ /// =================================================================================================
10764 public static int ToInt32 ( this Guid uuid )
10865 {
10966 var gb = uuid . ToByteArray ( ) ;
11067
11168 return BitConverter . ToInt32 ( gb , 0 ) ;
11269 }
11370
71+ /// -------------------------------------------------------------------------------------------------
11472 /// <summary>
115- /// Guid to long
73+ /// Guid to long.
11674 /// </summary>
117- /// <param name="uuid"></param>
118- /// <returns></returns>
75+ /// <param name="uuid">.</param>
76+ /// <returns>
77+ /// Uuid as a long.
78+ /// </returns>
79+ /// =================================================================================================
11980 public static long ToLong ( this Guid uuid )
12081 {
12182 var gb = uuid . ToByteArray ( ) ;
12283
12384 return BitConverter . ToInt64 ( gb , 0 ) ;
12485 }
86+
87+ /// -------------------------------------------------------------------------------------------------
88+ /// <summary>
89+ /// A Guid? extension method that query if 'source' is empty.
90+ /// </summary>
91+ /// <param name="source">Source Guid value to be checked.</param>
92+ /// <returns>
93+ /// True if empty, false if not.
94+ /// </returns>
95+ /// =================================================================================================
96+ public static bool IsEmpty ( this Guid ? source ) => source . IsNull ( ) || source == Guid . Empty ;
97+
98+ /// -------------------------------------------------------------------------------------------------
99+ /// <summary>
100+ /// A Guid? extension method that query if 'source' is empty.
101+ /// </summary>
102+ /// <param name="source">Source Guid value to be checked.</param>
103+ /// <returns>
104+ /// True if empty, false if not.
105+ /// </returns>
106+ /// =================================================================================================
107+ public static bool IsEmpty ( this Guid source ) => source == Guid . Empty ;
125108 }
126109}
0 commit comments