@@ -1690,8 +1690,7 @@ public static LuaTryGetMemberReturn TryGetMember(Expression target, Type targetT
1690
1690
var luaTargetType = LuaType . GetType ( targetType ) ;
1691
1691
var enumerateType = GetMethodEnumeratorType ( target , targetType ) ;
1692
1692
1693
- var memberEnum = luaTargetType . EnumerateMembers < MemberInfo > ( enumerateType , memberName , ignoreCase ) . GetEnumerator ( ) ;
1694
- try
1693
+ using ( var memberEnum = luaTargetType . EnumerateMembers < MemberInfo > ( enumerateType , memberName , ignoreCase ) . GetEnumerator ( ) )
1695
1694
{
1696
1695
if ( ! memberEnum . MoveNext ( ) ) // no member found
1697
1696
{
@@ -1764,10 +1763,6 @@ public static LuaTryGetMemberReturn TryGetMember(Expression target, Type targetT
1764
1763
}
1765
1764
}
1766
1765
}
1767
- finally
1768
- {
1769
- ( memberEnum as IDisposable ) ? . Dispose ( ) ;
1770
- }
1771
1766
} // func GetMember
1772
1767
1773
1768
private static LuaMethodEnumerate GetMethodEnumeratorType < TARG > ( TARG target , Func < TARG , Expression > getExpr , Func < TARG , Type > getType )
@@ -1793,34 +1788,35 @@ private static LuaMethodEnumerate GetMethodEnumeratorType(Expression target, Typ
1793
1788
public static LuaTrySetMemberReturn TrySetMember ( Expression target , Type targetType , string memberName , bool ignoreCase , Func < Type , Expression > set , out Expression result )
1794
1789
{
1795
1790
var luaType = LuaType . GetType ( targetType ) ;
1796
- var memberEnum = luaType . EnumerateMembers < MemberInfo > ( GetMethodEnumeratorType ( target , targetType ) , memberName , ignoreCase ) . GetEnumerator ( ) ;
1797
-
1798
- if ( ! memberEnum . MoveNext ( ) )
1791
+ using ( var memberEnum = luaType . EnumerateMembers < MemberInfo > ( GetMethodEnumeratorType ( target , targetType ) , memberName , ignoreCase ) . GetEnumerator ( ) )
1799
1792
{
1800
- result = null ;
1801
- return LuaTrySetMemberReturn . None ;
1802
- }
1793
+ if ( ! memberEnum . MoveNext ( ) )
1794
+ {
1795
+ result = null ;
1796
+ return LuaTrySetMemberReturn . None ;
1797
+ }
1803
1798
1804
- var memberInfo = memberEnum . Current ;
1805
- if ( memberInfo is PropertyInfo propertyInfo )
1806
- {
1807
- if ( ! propertyInfo . CanWrite )
1799
+ var memberInfo = memberEnum . Current ;
1800
+ if ( memberInfo is PropertyInfo propertyInfo )
1801
+ {
1802
+ if ( ! propertyInfo . CanWrite )
1803
+ {
1804
+ result = null ;
1805
+ return LuaTrySetMemberReturn . NotWritable ;
1806
+ }
1807
+ result = Expression . Assign ( Expression . Property ( target != null ? Lua . EnsureType ( target , targetType ) : null , propertyInfo ) , set ( propertyInfo . PropertyType ) ) ;
1808
+ return LuaTrySetMemberReturn . ValidExpression ;
1809
+ }
1810
+ else if ( memberInfo is FieldInfo fieldInfo )
1811
+ {
1812
+ result = Expression . Assign ( Expression . Field ( target != null ? Lua . EnsureType ( target , targetType ) : null , fieldInfo ) , set ( fieldInfo . FieldType ) ) ;
1813
+ return LuaTrySetMemberReturn . ValidExpression ;
1814
+ }
1815
+ else
1808
1816
{
1809
1817
result = null ;
1810
1818
return LuaTrySetMemberReturn . NotWritable ;
1811
1819
}
1812
- result = Expression . Assign ( Expression . Property ( target != null ? Lua . EnsureType ( target , targetType ) : null , propertyInfo ) , set ( propertyInfo . PropertyType ) ) ;
1813
- return LuaTrySetMemberReturn . ValidExpression ;
1814
- }
1815
- else if ( memberInfo is FieldInfo fieldInfo )
1816
- {
1817
- result = Expression . Assign ( Expression . Field ( target != null ? Lua . EnsureType ( target , targetType ) : null , fieldInfo ) , set ( fieldInfo . FieldType ) ) ;
1818
- return LuaTrySetMemberReturn . ValidExpression ;
1819
- }
1820
- else
1821
- {
1822
- result = null ;
1823
- return LuaTrySetMemberReturn . NotWritable ;
1824
1820
}
1825
1821
} // func TrySetMember
1826
1822
@@ -2561,42 +2557,43 @@ public static TMEMBERTYPE FindMember<TMEMBERTYPE, TARG>(IEnumerable<TMEMBERTYPE>
2561
2557
var memberMatchBind = new MemberMatchInfo < TMEMBERTYPE > ( unboundedArguments , arguments . Length ) ;
2562
2558
2563
2559
// get argument list
2564
- var memberEnum = members . GetEnumerator ( ) ;
2565
-
2566
- // reset the result with the first one
2567
- if ( memberEnum . MoveNext ( ) )
2568
- memberMatch . Reset ( memberEnum . Current , isMemberCall , memberMatchBind ) ;
2569
- else
2570
- return null ;
2571
-
2572
- // text the rest if there is better one
2573
- if ( memberEnum . MoveNext ( ) && ! memberMatchBind . IsPerfect )
2560
+ using ( var memberEnum = members . GetEnumerator ( ) )
2574
2561
{
2575
- var memberMatchCurrent = new MemberMatchInfo < TMEMBERTYPE > ( unboundedArguments , arguments . Length ) ;
2562
+ // reset the result with the first one
2563
+ if ( memberEnum . MoveNext ( ) )
2564
+ memberMatch . Reset ( memberEnum . Current , isMemberCall , memberMatchBind ) ;
2565
+ else
2566
+ return null ;
2576
2567
2577
- // test
2578
- memberMatch . Reset ( memberEnum . Current , isMemberCall , memberMatchCurrent ) ;
2579
- if ( memberMatchCurrent . IsBetter ( memberMatchBind ) )
2568
+ // text the rest if there is better one
2569
+ if ( memberEnum . MoveNext ( ) && ! memberMatchBind . IsPerfect )
2580
2570
{
2581
- memberMatchBind = memberMatchCurrent ;
2582
- memberMatchCurrent = new MemberMatchInfo < TMEMBERTYPE > ( unboundedArguments , arguments . Length ) ;
2583
- }
2571
+ var memberMatchCurrent = new MemberMatchInfo < TMEMBERTYPE > ( unboundedArguments , arguments . Length ) ;
2584
2572
2585
- while ( memberEnum . MoveNext ( ) && ! memberMatchBind . IsPerfect )
2586
- {
2587
2573
// test
2588
2574
memberMatch . Reset ( memberEnum . Current , isMemberCall , memberMatchCurrent ) ;
2589
2575
if ( memberMatchCurrent . IsBetter ( memberMatchBind ) )
2590
2576
{
2591
2577
memberMatchBind = memberMatchCurrent ;
2592
2578
memberMatchCurrent = new MemberMatchInfo < TMEMBERTYPE > ( unboundedArguments , arguments . Length ) ;
2593
2579
}
2580
+
2581
+ while ( memberEnum . MoveNext ( ) && ! memberMatchBind . IsPerfect )
2582
+ {
2583
+ // test
2584
+ memberMatch . Reset ( memberEnum . Current , isMemberCall , memberMatchCurrent ) ;
2585
+ if ( memberMatchCurrent . IsBetter ( memberMatchBind ) )
2586
+ {
2587
+ memberMatchBind = memberMatchCurrent ;
2588
+ memberMatchCurrent = new MemberMatchInfo < TMEMBERTYPE > ( unboundedArguments , arguments . Length ) ;
2589
+ }
2590
+ }
2594
2591
}
2595
- }
2596
2592
2597
- Debug . WriteLine ( "USED: {0}" , memberMatchBind . CurrentMember ) ;
2593
+ Debug . WriteLine ( "USED: {0}" , memberMatchBind . CurrentMember ) ;
2598
2594
2599
- return memberMatchBind . CurrentMember ;
2595
+ return memberMatchBind . CurrentMember ;
2596
+ }
2600
2597
} // func FindMember
2601
2598
2602
2599
private static MethodInfo MakeNonGenericMethod < TARG > ( MethodInfo mi , TARG [ ] arguments , Func < TARG , Type > getType )
@@ -2834,22 +2831,24 @@ private static IEnumerable<T> FilterNeeded<T>(IEnumerable<T> list, ReflectionFla
2834
2831
return list ;
2835
2832
} // func FilterNeeded
2836
2833
2837
- private static T GetOneResult < T > ( TypeInfo ti , string sName , ReflectionFlag flags , IEnumerable < T > list , [ CallerMemberName ] string sCaller = null )
2834
+ private static T GetOneResult < T > ( TypeInfo ti , string name , ReflectionFlag flags , IEnumerable < T > list , [ CallerMemberName ] string caller = null )
2838
2835
{
2839
2836
if ( ( flags & ReflectionFlag . NoException ) != 0 )
2840
2837
return list . FirstOrDefault ( ) ;
2841
2838
else
2842
2839
{
2843
- var e = list . GetEnumerator ( ) ;
2844
- if ( e . MoveNext ( ) ) // first element for return
2840
+ using ( var e = list . GetEnumerator ( ) )
2845
2841
{
2846
- var miFind = e . Current ;
2847
- if ( e . MoveNext ( ) )
2848
- throw new ArgumentException ( String . Format ( "{0} for {1}.{2}, is not unique." , sCaller , ti . Name , sName ) ) ;
2849
- return miFind ;
2842
+ if ( e . MoveNext ( ) ) // first element for return
2843
+ {
2844
+ var miFind = e . Current ;
2845
+ if ( e . MoveNext ( ) )
2846
+ throw new ArgumentException ( String . Format ( "{0} for {1}.{2}, is not unique." , caller , ti . Name , name ) ) ;
2847
+ return miFind ;
2848
+ }
2849
+ else
2850
+ throw new ArgumentException ( String . Format ( "{0} failed for {1}.{2}." , caller , ti . Name , name ) ) ;
2850
2851
}
2851
- else
2852
- throw new ArgumentException ( String . Format ( "{0} failed for {1}.{2}." , sCaller , ti . Name , sName ) ) ;
2853
2852
}
2854
2853
} // func GetOneResult
2855
2854
0 commit comments