31
31
using System . Diagnostics ;
32
32
using System . Collections . Generic ;
33
33
using System . Reflection ;
34
+ using System . Diagnostics . CodeAnalysis ;
34
35
35
36
namespace PdfSharpCore . Pdf
36
37
{
@@ -85,11 +86,13 @@ public string FixedValue
85
86
}
86
87
readonly string _fixedValue ;
87
88
89
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicConstructors | DynamicallyAccessedMemberTypes . NonPublicConstructors ) ]
88
90
public Type ObjectType
89
91
{
90
92
get { return _objectType ; }
91
93
set { _objectType = value ; }
92
94
}
95
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicConstructors | DynamicallyAccessedMemberTypes . NonPublicConstructors ) ]
93
96
Type _objectType ;
94
97
95
98
public bool CanBeIndirect
@@ -100,6 +103,7 @@ public bool CanBeIndirect
100
103
/// <summary>
101
104
/// Returns the type of the object to be created as value for the described key.
102
105
/// </summary>
106
+ [ return : DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicConstructors | DynamicallyAccessedMemberTypes . NonPublicConstructors ) ]
103
107
public Type GetValueType ( )
104
108
{
105
109
Type type = _objectType ;
@@ -180,8 +184,21 @@ public Type GetValueType()
180
184
/// </summary>
181
185
internal class DictionaryMeta
182
186
{
183
- public DictionaryMeta ( Type type )
187
+ public DictionaryMeta ( [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicFields ) ] Type type )
184
188
{
189
+ #if NET5_0_OR_GREATER
190
+ FieldInfo [ ] fields = type . GetFields ( BindingFlags . Public | BindingFlags . Static | BindingFlags . FlattenHierarchy ) ;
191
+ foreach ( FieldInfo field in fields )
192
+ {
193
+ var attributes = field . GetCustomAttributes < KeyInfoAttribute > ( false ) ;
194
+ foreach ( var attribute in attributes )
195
+ {
196
+ KeyDescriptor descriptor = new KeyDescriptor ( attribute ) ;
197
+ descriptor . KeyValue = ( string ) field . GetValue ( null ) ;
198
+ _keyDescriptors [ descriptor . KeyValue ] = descriptor ;
199
+ }
200
+ }
201
+ #else
185
202
// Rewritten for WinRT.
186
203
CollectKeyDescriptors ( type ) ;
187
204
//var fields = type.GetRuntimeFields(); // does not work
@@ -196,13 +213,14 @@ public DictionaryMeta(Type type)
196
213
// _keyDescriptors[descriptor.KeyValue] = descriptor;
197
214
// }
198
215
//}
216
+ #endif
199
217
}
200
218
201
219
// Background: The function GetRuntimeFields gets constant fields only for the specified type,
202
220
// not for its base types. So we have to walk recursively through base classes.
203
221
// The docmentation says full trust for the immediate caller is required for property BaseClass.
204
222
// TODO: Rewrite this stuff for medium trust.
205
- void CollectKeyDescriptors ( Type type )
223
+ void CollectKeyDescriptors ( [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . All ) ] Type type )
206
224
{
207
225
// Get fields of the specified type only.
208
226
var fields = type . GetTypeInfo ( ) . DeclaredFields ;
0 commit comments