@@ -23,34 +23,45 @@ static class ObjectParser {
23
23
var type = obj . GetType ( ) ;
24
24
var props = type . GetProperties ( ) ;
25
25
26
+ var properties = new List < ( string Name , string ? Value ) > ( ) ;
27
+
26
28
foreach ( var prop in props . Where ( x => IsAllowedProperty ( x . Name ) ) ) {
27
29
var val = prop . GetValue ( obj , null ) ;
28
30
29
31
if ( val == null ) continue ;
30
32
31
- yield return prop . PropertyType . IsArray
32
- ? GetArray ( prop , val )
33
- : GetValue ( prop , val ) ;
33
+ if ( prop . PropertyType . IsArray )
34
+ properties . AddRange ( GetArray ( prop , val ) ) ;
35
+ else
36
+ properties . Add ( GetValue ( prop , val ) ) ;
34
37
}
35
38
36
39
string ? ParseValue ( string ? format , object ? value ) => format == null ? value ? . ToString ( ) : string . Format ( $ "{{0:{ format } }}", value ) ;
37
40
38
- ( string , string ? ) GetArray ( PropertyInfo propertyInfo , object ? value ) {
41
+ IEnumerable < ( string , string ? ) > GetArray ( PropertyInfo propertyInfo , object ? value ) {
39
42
var elementType = propertyInfo . PropertyType . GetElementType ( ) ;
40
43
var array = ( Array ) value ! ;
41
44
42
45
var attribute = propertyInfo . GetCustomAttribute < RequestPropertyAttribute > ( ) ;
43
46
var name = attribute ? . Name ?? propertyInfo . Name ;
44
47
48
+ var queryType = attribute ? . ArrayQueryType ?? RequestArrayQueryType . CommaSeparated ;
49
+
45
50
if ( array . Length > 0 && elementType != null ) {
46
51
// convert the array to an array of strings
47
52
var values = array
48
53
. Cast < object > ( )
49
54
. Select ( item => ParseValue ( attribute ? . Format , item ) ) ;
50
- return ( name , string . Join ( "," , values ) ) ;
55
+
56
+ return queryType switch {
57
+ RequestArrayQueryType . CommaSeparated => new ( string , string ? ) [ ] { ( name , string . Join ( "," , values ) ) } ,
58
+ RequestArrayQueryType . ArrayParameters => values . Select ( x => ( $ "{ name } []", x ) ) ,
59
+ _ => throw new ArgumentOutOfRangeException ( )
60
+ } ;
61
+
51
62
}
52
63
53
- return ( name , null ) ;
64
+ return new ( string , string ? ) [ ] { ( name , null ) } ;
54
65
}
55
66
56
67
( string , string ? ) GetValue ( PropertyInfo propertyInfo , object ? value ) {
@@ -62,12 +73,16 @@ static class ObjectParser {
62
73
63
74
bool IsAllowedProperty ( string propertyName )
64
75
=> includedProperties . Length == 0 || includedProperties . Length > 0 && includedProperties . Contains ( propertyName ) ;
76
+
77
+ return properties ;
65
78
}
66
79
}
67
80
68
81
[ AttributeUsage ( AttributeTargets . Property ) ]
69
82
public class RequestPropertyAttribute : Attribute {
70
- public string ? Name { get ; set ; }
71
-
72
- public string ? Format { get ; set ; }
83
+ public string ? Name { get ; set ; }
84
+ public string ? Format { get ; set ; }
85
+ public RequestArrayQueryType ArrayQueryType { get ; set ; } = RequestArrayQueryType . CommaSeparated ;
73
86
}
87
+
88
+ public enum RequestArrayQueryType { CommaSeparated , ArrayParameters }
0 commit comments