3131using System . Diagnostics ;
3232using System . Collections . Generic ;
3333using System . Reflection ;
34+ using System . Diagnostics . CodeAnalysis ;
3435
3536namespace PdfSharpCore . Pdf
3637{
@@ -85,11 +86,13 @@ public string FixedValue
8586 }
8687 readonly string _fixedValue ;
8788
89+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicConstructors | DynamicallyAccessedMemberTypes . NonPublicConstructors ) ]
8890 public Type ObjectType
8991 {
9092 get { return _objectType ; }
9193 set { _objectType = value ; }
9294 }
95+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicConstructors | DynamicallyAccessedMemberTypes . NonPublicConstructors ) ]
9396 Type _objectType ;
9497
9598 public bool CanBeIndirect
@@ -100,6 +103,7 @@ public bool CanBeIndirect
100103 /// <summary>
101104 /// Returns the type of the object to be created as value for the described key.
102105 /// </summary>
106+ [ return : DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicConstructors | DynamicallyAccessedMemberTypes . NonPublicConstructors ) ]
103107 public Type GetValueType ( )
104108 {
105109 Type type = _objectType ;
@@ -180,8 +184,21 @@ public Type GetValueType()
180184 /// </summary>
181185 internal class DictionaryMeta
182186 {
183- public DictionaryMeta ( Type type )
187+ public DictionaryMeta ( [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicFields ) ] Type type )
184188 {
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
185202 // Rewritten for WinRT.
186203 CollectKeyDescriptors ( type ) ;
187204 //var fields = type.GetRuntimeFields(); // does not work
@@ -196,8 +213,10 @@ public DictionaryMeta(Type type)
196213 // _keyDescriptors[descriptor.KeyValue] = descriptor;
197214 // }
198215 //}
216+ #endif
199217 }
200218
219+ #if ! NET5_0_OR_GREATER
201220 // Background: The function GetRuntimeFields gets constant fields only for the specified type,
202221 // not for its base types. So we have to walk recursively through base classes.
203222 // The docmentation says full trust for the immediate caller is required for property BaseClass.
@@ -220,6 +239,7 @@ void CollectKeyDescriptors(Type type)
220239 if ( type != typeof ( object ) && type != typeof ( PdfObject ) )
221240 CollectKeyDescriptors ( type ) ;
222241 }
242+ #endif
223243
224244 /// <summary>
225245 /// Gets the KeyDescriptor of the specified key, or null if no such descriptor exits.
0 commit comments