88using System . Linq . Expressions ;
99using System . Runtime . CompilerServices ;
1010using System . Threading ;
11+ using System . Xml . Linq ;
1112using static System . Linq . Expressions . Expression ;
1213
1314namespace Open . Text ;
@@ -94,7 +95,7 @@ static Func<TEnum, string> GetEnumNameDelegate()
9495 internal static Func < TEnum , string > NameLookup
9596 => LazyInitializer . EnsureInitialized ( ref _nameLookup ,
9697 ( ) => GetEnumNameDelegate ( ) ) ! ;
97- internal static Func < string , ( bool Success , TEnum Value ) > GetEnumTryParseDelegate ( )
98+ internal static Func < string , ValueLookupResult > GetEnumTryParseDelegate ( )
9899 {
99100 var valueParam = Parameter ( typeof ( string ) , "value" ) ;
100101 var defaultExpression = CreateNewTuple ( false , default ! ) ;
@@ -115,7 +116,7 @@ internal static Func<TEnum, string> NameLookup
115116 )
116117 ) . ToArray ( ) ;
117118
118- return Lambda < Func < string , ( bool Success , TEnum Value ) > > (
119+ return Lambda < Func < string , ValueLookupResult > > (
119120 Switch (
120121 Property ( valueParam , nameof ( string . Length ) ) ,
121122 defaultExpression ,
@@ -127,14 +128,33 @@ internal static Func<TEnum, string> NameLookup
127128
128129 static Expression CreateNewTuple ( bool success , TEnum value )
129130 => New (
130- typeof ( ( bool Success , TEnum Value ) ) . GetConstructor ( new [ ] { typeof ( bool ) , typeof ( TEnum ) } ) ,
131+ typeof ( ValueLookupResult ) . GetConstructor ( new [ ] { typeof ( bool ) , typeof ( TEnum ) } ) ,
131132 Constant ( success ) ,
132133 Constant ( value )
133134 ) ;
134135 }
135136
136- private static Func < string , ( bool Success , TEnum Value ) > ? _valueLookup ;
137- internal static Func < string , ( bool Success , TEnum Value ) > ValueLookup
137+ internal readonly struct ValueLookupResult
138+ {
139+ public ValueLookupResult ( bool success , TEnum value )
140+ {
141+ Success = success ;
142+ Value = value ;
143+ }
144+
145+ public bool Success { get ; }
146+ public TEnum Value { get ; }
147+
148+ public void Deconstruct ( out bool success , out TEnum value )
149+ {
150+ success = Success ;
151+ value = Value ;
152+ }
153+
154+ }
155+
156+ private static Func < string , ValueLookupResult > ? _valueLookup ;
157+ internal static Func < string , ValueLookupResult > ValueLookup
138158 => LazyInitializer . EnsureInitialized ( ref _valueLookup ,
139159 ( ) => GetEnumTryParseDelegate ( ) ) ! ;
140160
@@ -477,9 +497,9 @@ public static TEnum Parse<TEnum>(string value)
477497 public static bool TryParse < TEnum > ( string value , out TEnum e )
478498 where TEnum : Enum
479499 {
480- var ( success , result ) = EnumValue < TEnum > . ValueLookup ( value ) ;
481- e = result ;
482- return success ;
500+ var result = EnumValue < TEnum > . ValueLookup ( value ) ;
501+ e = result . Value ;
502+ return result . Success ;
483503 }
484504
485505 /// <returns>The enum that represents the string <paramref name="value"/> provided.</returns>
0 commit comments