@@ -81,7 +81,8 @@ public static IEnumerable<KeyValuePair<IntStringKey, PhpValue>> EnumerateVisible
81
81
return EnumerateInstanceFields ( instance ,
82
82
( p ) => new IntStringKey ( p . PropertyName ) ,
83
83
FuncExtensions . Identity < IntStringKey > ( ) ,
84
- ( m ) => m . IsVisible ( caller ) ) ;
84
+ ( m ) => m . IsVisible ( caller )
85
+ ) ;
85
86
}
86
87
87
88
/// <summary>
@@ -93,9 +94,23 @@ public static IEnumerable<KeyValuePair<string, PhpValue>> EnumerateInstanceField
93
94
{
94
95
return EnumerateInstanceFields ( instance ,
95
96
s_formatPropertyNameForPrint ,
96
- s_keyToString ) ;
97
+ s_keyToString ,
98
+ getValue : s_getValueWithTryCatch
99
+ ) ;
97
100
}
98
101
102
+ static readonly Func < PhpPropertyInfo , object , PhpValue > s_getValueWithTryCatch = ( p , instance ) =>
103
+ {
104
+ try
105
+ {
106
+ return p . GetValue ( null , instance ) ;
107
+ }
108
+ catch ( Exception e )
109
+ {
110
+ return $ "Property '{ p . PropertyName } ' threw an exception of type { e . GetType ( ) . Name } : { e . Message } ";
111
+ }
112
+ } ;
113
+
99
114
public static readonly Func < IntStringKey , string > s_keyToString = k => k . ToString ( ) ;
100
115
101
116
public static readonly Func < PhpPropertyInfo , string > s_propertyName = p => p . PropertyName ;
@@ -127,7 +142,9 @@ public static IEnumerable<KeyValuePair<string, PhpValue>> EnumerateInstanceField
127
142
{
128
143
return EnumerateInstanceFields ( instance ,
129
144
s_formatPropertyNameForDump ,
130
- s_keyToString ) ;
145
+ s_keyToString ,
146
+ getValue : s_getValueWithTryCatch
147
+ ) ;
131
148
}
132
149
133
150
/// <summary>
@@ -170,9 +187,10 @@ public static IEnumerable<KeyValuePair<string, PhpValue>> EnumerateInstanceField
170
187
/// <param name="keyFormatter2">Function converting </param>
171
188
/// <param name="predicate">Optional. Predicate filtering fields.</param>
172
189
/// <param name="ignoreRuntimeFields">Whether to ignore listing runtime fields.</param>
190
+ /// <param name="getValue">Function getting property value on given <paramref name="instance"/>.</param>
173
191
/// <returns>Enumeration of fields and their values, including runtime fields.</returns>
174
192
/// <typeparam name="TKey">Enumerated pairs key. Usually <see cref="IntStringKey"/>.</typeparam>
175
- public static IEnumerable < KeyValuePair < TKey , PhpValue > > EnumerateInstanceFields < TKey > ( object instance , Func < PhpPropertyInfo , TKey > keyFormatter , Func < IntStringKey , TKey > keyFormatter2 , Func < PhpPropertyInfo , bool > predicate = null , bool ignoreRuntimeFields = false )
193
+ public static IEnumerable < KeyValuePair < TKey , PhpValue > > EnumerateInstanceFields < TKey > ( object instance , Func < PhpPropertyInfo , TKey > keyFormatter , Func < IntStringKey , TKey > keyFormatter2 , Func < PhpPropertyInfo , bool > predicate = null , bool ignoreRuntimeFields = false , Func < PhpPropertyInfo , object , PhpValue > getValue = null )
176
194
{
177
195
Debug . Assert ( instance != null ) ;
178
196
@@ -189,7 +207,8 @@ public static IEnumerable<KeyValuePair<TKey, PhpValue>> EnumerateInstanceFields<
189
207
{
190
208
yield return new KeyValuePair < TKey , PhpValue > (
191
209
keyFormatter ( p ) ,
192
- p . GetValue ( null , instance ) ) ;
210
+ getValue != null ? getValue ( p , instance ) : p . GetValue ( null , instance )
211
+ ) ;
193
212
}
194
213
}
195
214
}
0 commit comments