@@ -4,28 +4,45 @@ namespace Spotflow.InMemory.Azure.Internals;
44
55internal static class ReflectionUtils
66{
7-
87 public static TOut ? ReadInternalReferenceProperty < TOut > ( object obj , string propertyName ) where TOut : class
98 {
10- var value = ReadInternalProperty ( obj , propertyName ) ;
9+ if ( ! TryReadOptionalInternalReferenceProperty ( obj , propertyName , out TOut ? value ) )
10+ {
11+ throw new InvalidOperationException ( $ "Property '{ propertyName } ' not found on type { obj . GetType ( ) . Name } ") ;
12+ }
13+
14+ return value ;
15+ }
16+
17+ public static bool TryReadOptionalInternalReferenceProperty < TOut > ( object obj , string propertyName , out TOut ? value ) where TOut : class
18+ {
19+ value = null ;
1120
12- if ( value is null )
21+ if ( ! TryReadOptionalInternalProperty ( obj , propertyName , out var rawValue ) )
1322 {
14- return null ;
23+ return false ;
1524 }
1625
17- if ( ! value . GetType ( ) . IsAssignableTo ( typeof ( TOut ) ) )
26+ if ( rawValue is null )
1827 {
19- throw new InvalidOperationException ( $ "Property ' { propertyName } ' is not assignable to { typeof ( TOut ) . Name } " ) ;
28+ return true ; // null is a valid value for reference types
2029 }
2130
22- return ( TOut ? ) value ;
31+ if ( ! rawValue . GetType ( ) . IsAssignableTo ( typeof ( TOut ) ) )
32+ {
33+ throw new InvalidOperationException ( $ "Property '{ propertyName } ' is not assignable to { typeof ( TOut ) . Name } ") ;
34+ }
2335
36+ value = ( TOut ? ) rawValue ;
37+ return true ;
2438 }
2539
2640 public static TOut ReadInternalValueProperty < TOut > ( object obj , string propertyName ) where TOut : struct
2741 {
28- var value = ReadInternalProperty ( obj , propertyName ) ;
42+ if ( ! TryReadOptionalInternalProperty ( obj , propertyName , out var value ) )
43+ {
44+ throw new InvalidOperationException ( $ "Property '{ propertyName } ' not found on type { obj . GetType ( ) . Name } ") ;
45+ }
2946
3047 if ( value is not TOut )
3148 {
@@ -35,7 +52,7 @@ public static TOut ReadInternalValueProperty<TOut>(object obj, string propertyNa
3552 return ( TOut ) value ;
3653 }
3754
38- private static object ? ReadInternalProperty ( object obj , string propertyName )
55+ private static bool TryReadOptionalInternalProperty ( object obj , string propertyName , out object ? value )
3956 {
4057 ArgumentNullException . ThrowIfNull ( obj ) ;
4158 ArgumentException . ThrowIfNullOrWhiteSpace ( propertyName ) ;
@@ -46,10 +63,12 @@ public static TOut ReadInternalValueProperty<TOut>(object obj, string propertyNa
4663
4764 if ( property is null )
4865 {
49- throw new InvalidOperationException ( $ "Property '{ propertyName } ' not found on type { obj . GetType ( ) . Name } ") ;
66+ value = null ;
67+ return false ;
5068 }
5169
52- return property . GetValue ( obj ) ;
70+ value = property . GetValue ( obj ) ;
71+ return true ;
5372 }
5473
5574}
0 commit comments