@@ -20,85 +20,87 @@ public static CsDeclaration Create(Type type)
20
20
if ( type . IsEnum )
21
21
{
22
22
return new CsEnum ( type )
23
- {
24
- Values = type . GetFields ( )
25
- . Where ( f => f . Name != "value__" )
26
- . Select ( y => new CsEnumValue
27
- {
28
- Name = y . Name ,
29
- Value =
30
- Convert
31
- . ToInt32 ( y . GetRawConstantValue ( ) )
32
- } )
33
- . ToArray ( )
34
- } ;
23
+ {
24
+ Values = type . GetFields ( )
25
+ . Where ( f => f . Name != "value__" )
26
+ . Select ( y => new CsEnumValue
27
+ {
28
+ Name = y . Name ,
29
+ Value =
30
+ Convert
31
+ . ToInt32 ( y . GetRawConstantValue ( ) )
32
+ } )
33
+ . ToArray ( )
34
+ } ;
35
35
}
36
36
37
37
if ( type . IsClass )
38
38
{
39
39
return new CsClass ( type )
40
- {
41
- TypeParameters = type . IsGenericType
42
- ? type . GetGenericTypeDefinition ( )
43
- . GetGenericArguments ( )
44
- . Select ( x => x . Name )
45
- . ToArray ( )
46
- : Array . Empty < string > ( ) ,
47
- Members = type . GetDefinition ( )
48
- . GetMembers ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . DeclaredOnly )
49
- . Where ( m => ! typeof ( object )
50
- . GetMembers ( )
51
- . Select ( me => me . Name )
52
- . Contains ( m . Name ) )
53
- . Select ( Create )
54
- . Where ( x => x != null )
55
- . ToArray ( )
56
- } ;
40
+ {
41
+ TypeParameters = type . IsGenericType
42
+ ? type . GetGenericTypeDefinition ( )
43
+ . GetGenericArguments ( )
44
+ . Select ( x => x . Name )
45
+ . ToArray ( )
46
+ : Array . Empty < string > ( ) ,
47
+ Members = type . GetDefinition ( )
48
+ . GetMembers ( BindingFlags . Public | BindingFlags . Instance |
49
+ BindingFlags . DeclaredOnly | BindingFlags . Static )
50
+ . Where ( m => ! typeof ( object )
51
+ . GetMembers ( )
52
+ . Select ( me => me . Name )
53
+ . Contains ( m . Name ) )
54
+ . Select ( Create )
55
+ . Where ( x => x != null )
56
+ . ToArray ( )
57
+ } ;
57
58
}
58
59
59
60
if ( type . IsInterface )
60
61
{
61
62
return new CsInterface ( type )
62
- {
63
- TypeParameters = type . IsGenericType
64
- ? type . GetGenericTypeDefinition ( )
65
- . GetGenericArguments ( )
66
- . Select ( x => x . Name )
67
- . ToArray ( )
68
- : Array . Empty < string > ( ) ,
69
- Members = type . GetDefinition ( )
70
- . GetMembers ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . DeclaredOnly )
71
- . Where ( m => ! typeof ( object )
72
- . GetMembers ( )
73
- . Select ( me => me . Name )
74
- . Contains ( m . Name ) )
75
- . Select ( Create )
76
- . Where ( x => x != null )
77
- . ToArray ( )
78
- } ;
63
+ {
64
+ TypeParameters = type . IsGenericType
65
+ ? type . GetGenericTypeDefinition ( )
66
+ . GetGenericArguments ( )
67
+ . Select ( x => x . Name )
68
+ . ToArray ( )
69
+ : Array . Empty < string > ( ) ,
70
+ Members = type . GetDefinition ( )
71
+ . GetMembers ( BindingFlags . Public | BindingFlags . Instance |
72
+ BindingFlags . DeclaredOnly )
73
+ . Where ( m => ! typeof ( object )
74
+ . GetMembers ( )
75
+ . Select ( me => me . Name )
76
+ . Contains ( m . Name ) )
77
+ . Select ( Create )
78
+ . Where ( x => x != null )
79
+ . ToArray ( )
80
+ } ;
79
81
}
80
82
81
83
var isStruct = type . IsValueType && ! type . IsEnum && ! type . IsPrimitive ;
82
84
if ( isStruct )
83
85
{
84
86
return new CsStruct ( type )
85
- {
86
- TypeParameters = type . IsGenericType
87
- ? type . GetGenericTypeDefinition ( )
88
- . GetGenericArguments ( )
89
- . Select ( x => x . Name )
90
- . ToArray ( )
91
- : Array . Empty < string > ( ) ,
92
- Members = type . GetDefinition ( )
93
- . GetMembers ( )
94
- . Where ( m => ! typeof ( object )
95
- . GetMembers ( )
96
- . Select ( me => me . Name )
97
- . Contains ( m . Name ) )
98
- . Select ( Create )
99
- . Where ( x => x != null )
100
- . ToArray ( )
101
- } ;
87
+ {
88
+ TypeParameters = type . IsGenericType
89
+ ? type . GetGenericTypeDefinition ( )
90
+ . GetGenericArguments ( )
91
+ . Select ( x => x . Name )
92
+ . ToArray ( )
93
+ : Array . Empty < string > ( ) ,
94
+ Members = type . GetDefinition ( )
95
+ . GetMembers ( )
96
+ . Where ( m => ! typeof ( object )
97
+ . GetMembers ( )
98
+ . Select ( me => me . Name )
99
+ . Contains ( m . Name ) )
100
+ . Select ( Create )
101
+ . Where ( x => x != null )
102
+ . ToArray ( )
103
+ } ;
102
104
}
103
105
104
106
return null ;
@@ -112,8 +114,9 @@ private static CsTypeMember Create(MemberInfo memberInfo)
112
114
return Create ( methodInfo ) ;
113
115
case PropertyInfo propertyInfo :
114
116
return Create ( propertyInfo ) ;
117
+ case FieldInfo fieldInfo :
118
+ return Create ( fieldInfo ) ;
115
119
case TypeInfo _:
116
- case FieldInfo _:
117
120
case EventInfo _:
118
121
return null ;
119
122
default :
@@ -122,33 +125,77 @@ private static CsTypeMember Create(MemberInfo memberInfo)
122
125
}
123
126
}
124
127
128
+ private static CsTypeMember Create ( FieldInfo fieldInfo )
129
+ {
130
+ CsLiteral ? GetLiteral ( Type fieldType )
131
+ {
132
+ if ( ! fieldInfo . IsLiteral )
133
+ {
134
+ return null ;
135
+ }
136
+
137
+ var constantValue = fieldInfo . GetRawConstantValue ( ) ;
138
+
139
+ if ( fieldType == typeof ( string )
140
+ || fieldType == typeof ( int )
141
+ || fieldType == typeof ( bool )
142
+ || fieldType . IsEnum )
143
+
144
+ {
145
+ return new CsLiteral { Value = constantValue , Type = new CsType ( fieldType ) } ;
146
+ }
147
+
148
+ return null ;
149
+ }
150
+
151
+ return new CsField
152
+ {
153
+ Name = fieldInfo . Name ,
154
+ IsStatic = fieldInfo . IsStatic ,
155
+ IsInherited = fieldInfo . DeclaringType != fieldInfo . ReflectedType ,
156
+ AccessModifier = GetAccessModifier ( fieldInfo . IsPrivate ,
157
+ fieldInfo . IsFamily ,
158
+ fieldInfo . IsPublic ,
159
+ fieldInfo . IsAssembly ) ,
160
+ Attributes = fieldInfo . CustomAttributes
161
+ . Select ( x => new CsAttribute
162
+ {
163
+ Name = x . AttributeType . Name ,
164
+ OriginalType = x . AttributeType
165
+ } )
166
+ . ToArray ( ) ,
167
+ Type = new CsType ( fieldInfo . FieldType ) ,
168
+ Value = GetLiteral ( fieldInfo . FieldType )
169
+ } ;
170
+ }
171
+
125
172
private static CsMethod Create ( MethodInfo methodInfo )
126
173
{
127
174
if ( methodInfo . IsSpecialName )
128
175
return null ;
129
176
130
177
return new CsMethod
131
- {
132
- Name = methodInfo . Name ,
133
- IsStatic = methodInfo . IsStatic ,
134
- IsInherited = methodInfo . DeclaringType != methodInfo . ReflectedType ,
135
- AccessModifier = GetAccessModifier ( methodInfo . IsPrivate ,
136
- methodInfo . IsFamily ,
137
- methodInfo . IsPublic ,
138
- methodInfo . IsAssembly ) ,
139
- Attributes = methodInfo . CustomAttributes
140
- . Select ( x => new CsAttribute
141
- {
142
- Name = x . AttributeType . Name ,
143
- OriginalType = x . AttributeType
144
- } )
145
- . ToArray ( ) ,
146
- ReturnType = new CsType ( methodInfo . ReturnType ) ,
147
- Parameters = methodInfo . GetParameters ( )
148
- . Select ( Create )
149
- . ToArray ( ) ,
150
- OriginalMethod = methodInfo
151
- } ;
178
+ {
179
+ Name = methodInfo . Name ,
180
+ IsStatic = methodInfo . IsStatic ,
181
+ IsInherited = methodInfo . DeclaringType != methodInfo . ReflectedType ,
182
+ AccessModifier = GetAccessModifier ( methodInfo . IsPrivate ,
183
+ methodInfo . IsFamily ,
184
+ methodInfo . IsPublic ,
185
+ methodInfo . IsAssembly ) ,
186
+ Attributes = methodInfo . CustomAttributes
187
+ . Select ( x => new CsAttribute
188
+ {
189
+ Name = x . AttributeType . Name ,
190
+ OriginalType = x . AttributeType
191
+ } )
192
+ . ToArray ( ) ,
193
+ ReturnType = new CsType ( methodInfo . ReturnType ) ,
194
+ Parameters = methodInfo . GetParameters ( )
195
+ . Select ( Create )
196
+ . ToArray ( ) ,
197
+ OriginalMethod = methodInfo
198
+ } ;
152
199
}
153
200
154
201
private static CsProperty Create ( PropertyInfo propertyInfo )
@@ -166,23 +213,23 @@ private static CsProperty Create(PropertyInfo propertyInfo)
166
213
}
167
214
168
215
return new CsProperty
169
- {
170
- Name = propertyInfo . Name ,
171
- IsStatic = getMethod . IsStatic ,
172
- IsInherited = propertyInfo . DeclaringType != propertyInfo . ReflectedType ,
173
- AccessModifier = GetAccessModifier ( getMethod . IsPrivate ,
174
- getMethod . IsFamily ,
175
- getMethod . IsPublic ,
176
- getMethod . IsAssembly ) ,
177
- Attributes = propertyInfo . CustomAttributes
178
- . Select ( x => new CsAttribute
179
- {
180
- Name = x . AttributeType . Name ,
181
- OriginalType = x . AttributeType
182
- } )
183
- . ToArray ( ) ,
184
- Type = new CsType ( propertyInfo . PropertyType )
185
- } ;
216
+ {
217
+ Name = propertyInfo . Name ,
218
+ IsStatic = getMethod . IsStatic ,
219
+ IsInherited = propertyInfo . DeclaringType != propertyInfo . ReflectedType ,
220
+ AccessModifier = GetAccessModifier ( getMethod . IsPrivate ,
221
+ getMethod . IsFamily ,
222
+ getMethod . IsPublic ,
223
+ getMethod . IsAssembly ) ,
224
+ Attributes = propertyInfo . CustomAttributes
225
+ . Select ( x => new CsAttribute
226
+ {
227
+ Name = x . AttributeType . Name ,
228
+ OriginalType = x . AttributeType
229
+ } )
230
+ . ToArray ( ) ,
231
+ Type = new CsType ( propertyInfo . PropertyType )
232
+ } ;
186
233
}
187
234
188
235
private static CsMethodParameter Create ( ParameterInfo x )
0 commit comments