Search code examples
c#reflection.net-8.0getpropertiesnative-aot

NET8 NativeAOT: Type.GetProperties() doesn't work for some types but does for others! why?


I have a program which was successfully published with NET8 NativeAOT, but Type.GetProperties() works for some class where properties r returned properly, but not for others, where it returns no property at all. i don't know why nor find a clue to debug. Can someone give some hints?

it doesn't work for a simple class like :


    public sealed partial class UILogText(string name = "", string text = "") 
    {
        public string Name { get; set; } = name;
        public string Text { get; set; } = text;

        public override string ToString() => Text;
    }

but works well for some complicated class like :

 public sealed partial class GatewayInfo : GatewayEntry, IEntryInfo 
    { 
       //39 public properties (note:not fields) ...
    }

I know nothing more can i do with it.

I know NativeAOT has problems with reflections in some cases, but the problem here is that it already worked for some classes. I can't tell the key difference between above mentioned exmaples.

Can someone give a hint on what's happening here? or any clue to find out?


Solution

  • [method: DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties, typeof(UILogText))]
        public sealed partial class UILogText(string name = "", string text = "") 
        {
            public string Name { get; set; } = name;
            public string Text { get; set; } = text;
    
            public override string ToString() => Text;
        }
    

    According to comment above, add DynamicDependencyAttribute solved my problem!