c# - Test if string is a guid without throwing exceptions? -
i want try convert string guid, don't want rely on catching exceptions (
- for performance reasons - exceptions expensive
- for usability reasons - debugger pops
- for design reasons - expected not exceptional
in other words code:
public static boolean trystrtoguid(string s, out guid value) { try { value = new guid(s); return true; } catch (formatexception) { value = guid.empty; return false; } }
is not suitable.
i try using regex, since guid can parenthesis wrapped, brace wrapped, none wrapped, makes hard.
additionally, thought guid values invalid(?)
update 1
christiank had idea catch formatexception
, rather all. changed question's code sample include suggestion.
update 2
why worry thrown exceptions? expecting invalid guids often?
the answer yes. why using trystrtoguid - am expecting bad data.
example 1 namespace extensions can specified appending guid folder name. might parsing folder names, checking see if text after final . guid.
c:\program files c:\program files.old c:\users c:\users.old c:\usermanager.{ce7f5aa5-6832-43fe-bae1-80d14cd8f666} c:\windows c:\windows.old
example 2 might running heavily used web-server wants check validity of posted data. don't want invalid data tying resources 2-3 orders of magnitude higher needs be.
example 3 might parsing search expression entered user.
if enter guid's want process them specially (such searching object, or highlight , format specific search term in response text.)
update 3 - performance benchmarks
test converting 10,000 guids, , 10,000 bad guids.
catch formatexception: 10,000 good: 63,668 ticks 10,000 bad: 6,435,609 ticks regex pre-screen try-catch: 10,000 good: 637,633 ticks 10,000 bad: 717,894 ticks com interop clsidfromstring 10,000 good: 126,120 ticks 10,000 bad: 23,134 ticks
p.s. shouldn't have justify question.
performance benchmarks
catch exception: 10,000 good: 63,668 ticks 10,000 bad: 6,435,609 ticks regex pre-screen: 10,000 good: 637,633 ticks 10,000 bad: 717,894 ticks com interop clsidfromstring 10,000 good: 126,120 ticks 10,000 bad: 23,134 ticks
com intertop (fastest) answer:
/// <summary> /// attempts convert string guid. /// </summary> /// <param name="s">the string try convert</param> /// <param name="value">upon return contain guid</param> /// <returns>returns true if successful, otherwise false</returns> public static boolean trystrtoguid(string s, out guid value) { //clsidfromstring returns empty guid null strings if ((s == null) || (s == "")) { value = guid.empty; return false; } int hresult = pinvoke.objbase.clsidfromstring(s, out value); if (hresult >= 0) { return true; } else { value = guid.empty; return false; } } namespace pinvoke { class objbase { /// <summary> /// function converts string generated stringfromclsid function original class identifier. /// </summary> /// <param name="sz">string represents class identifier</param> /// <param name="clsid">on return contain class identifier</param> /// <returns> /// positive or 0 if class identifier obtained /// negative if call failed /// </returns> [dllimport("ole32.dll", charset = charset.unicode, exactspelling = true, preservesig = true)] public static extern int clsidfromstring(string sz, out guid clsid); } }
bottom line: if need check if string guid, , care performance, use com interop.
if need convert guid in string representation guid, use
new guid(somestring);
Comments
Post a Comment