File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed
Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -197,6 +197,21 @@ public void It_Should_Convert_An_Object()
197197 Assert . Equal ( LiquidString . Create ( ( String ) testClass . ObjField1 ) , objAsHash [ "objfield1" ] . Value ) ;
198198 }
199199
200+ [ Fact ]
201+ public void It_Should_Ignore_Static_Properties ( )
202+ {
203+ ClassWithStaticProperty . StaticField1 = "aaa" ;
204+ var testClass = new ClassWithStaticProperty { Field1 = "bbb" } ;
205+
206+ var result = _converter . Convert ( testClass ) ;
207+ Assert . True ( result . HasValue ) ;
208+ var objAsHash = ( LiquidHash ) result . Value ;
209+
210+ Assert . Equal ( 1 , objAsHash . Keys . Count ) ;
211+ Assert . True ( objAsHash . ContainsKey ( "field1" ) ) ;
212+ Assert . Equal ( LiquidString . Create ( testClass . Field1 ) , objAsHash [ "field1" ] . Value ) ;
213+ }
214+
200215 [ Fact ]
201216 public void It_Should_Convert_A_Nested_Object ( )
202217 {
@@ -310,5 +325,10 @@ public class ClassWithAttributes
310325
311326 }
312327
328+ public class ClassWithStaticProperty
329+ {
330+ public String Field1 { get ; set ; }
331+ public static String StaticField1 { get ; set ; }
332+ }
313333 }
314334}
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ namespace Liquid.NET.Utils
1010{
1111 public class LiquidValueConverter
1212 {
13+ public static readonly BindingFlags PublicInstancePropertiesWithAGetter = BindingFlags . Public | BindingFlags . Instance | BindingFlags . GetProperty ;
14+
1315 public Option < ILiquidValue > Convert ( Object obj )
1416 {
1517 if ( ReferenceEquals ( obj , null ) )
@@ -36,7 +38,7 @@ private Option<ILiquidValue> FromObject(Object obj)
3638 {
3739 var newHash = new LiquidHash ( ) ;
3840 var kvps = obj . GetType ( )
39- . GetProperties ( )
41+ . GetProperties ( PublicInstancePropertiesWithAGetter )
4042 . Where ( property => ! ( property . GetCustomAttributes < LiquidIgnoreAttribute > ( ) . Any ( )
4143 || ( property . GetCustomAttributes < LiquidIgnoreIfNullAttribute > ( ) . Any ( )
4244 && ReferenceEquals ( property . GetGetMethod ( ) . Invoke ( obj , null ) , null ) ) ) )
You can’t perform that action at this time.
0 commit comments