Commit c1a9771 1 parent 3703828 commit c1a9771 Copy full SHA for c1a9771
File tree 2 files changed +40
-9
lines changed
src/System.Linq.Dynamic.Core
test/System.Linq.Dynamic.Core.Tests.Net6
2 files changed +40
-9
lines changed Original file line number Diff line number Diff line change @@ -18,24 +18,28 @@ namespace System.Linq.Dynamic.Core;
18
18
/// </summary>
19
19
public abstract class DynamicClass : DynamicObject
20
20
{
21
- private readonly Dictionary < string , object ? > _propertiesDictionary = new ( ) ;
21
+ private Dictionary < string , object ? > ? _propertiesDictionary = null ;
22
22
23
23
private Dictionary < string , object ? > Properties
24
24
{
25
25
get
26
26
{
27
- foreach ( PropertyInfo pi in GetType ( ) . GetProperties ( BindingFlags . Public | BindingFlags . Instance ) )
27
+ if ( _propertiesDictionary == null )
28
28
{
29
- int parameters = pi . GetIndexParameters ( ) . Length ;
30
- if ( parameters > 0 )
29
+ _propertiesDictionary = new ( ) ;
30
+ foreach ( PropertyInfo pi in GetType ( ) . GetProperties ( BindingFlags . Public | BindingFlags . Instance ) )
31
31
{
32
- // The property is an indexer, skip this.
33
- continue ;
32
+ int parameters = pi . GetIndexParameters ( ) . Length ;
33
+ if ( parameters > 0 )
34
+ {
35
+ // The property is an indexer, skip this.
36
+ continue ;
37
+ }
38
+
39
+ _propertiesDictionary . Add ( pi . Name , pi . GetValue ( this , null ) ) ;
34
40
}
35
-
36
- _propertiesDictionary . Add ( pi . Name , pi . GetValue ( this , null ) ) ;
37
41
}
38
-
42
+
39
43
return _propertiesDictionary ;
40
44
}
41
45
}
Original file line number Diff line number Diff line change
1
+ using System . Collections . Generic ;
2
+ using FluentAssertions ;
3
+ using Xunit ;
4
+
5
+ namespace System . Linq . Dynamic . Core . Tests
6
+ {
7
+ public class DynamicClassTest
8
+ {
9
+ [ Fact ]
10
+ public void GetPropertiesWorks ( )
11
+ {
12
+ // Arrange
13
+ var range = new List < object >
14
+ {
15
+ new { FieldName = "TestFieldName" , Value = 3.14159 }
16
+ } ;
17
+
18
+ // Act
19
+ var rangeResult = range . AsQueryable ( ) . Select ( "new(FieldName as FieldName)" ) . ToDynamicList ( ) ;
20
+ var item = rangeResult . FirstOrDefault ( ) ;
21
+
22
+ var call = ( ) => item . GetDynamicMemberNames ( ) ;
23
+ call . Should ( ) . NotThrow ( ) ;
24
+ call . Should ( ) . NotThrow ( ) ;
25
+ }
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments