Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native AOT warnings #4065

Closed
IEvangelist opened this issue Jan 26, 2024 · 11 comments · Fixed by microsoft/kiota-dotnet#184
Closed

Native AOT warnings #4065

IEvangelist opened this issue Jan 26, 2024 · 11 comments · Fixed by microsoft/kiota-dotnet#184
Assignees
Labels
Csharp Pull requests that update .net code Needs: Attention 👋
Milestone

Comments

@IEvangelist
Copy link
Member

One of the most compelling reasons for using this library is that it's Native-AOT compatible. However, I have an app that is consuming a client library generated by this library, and it's causing AOT warnings:

p/home/runner/.nuget/packages/microsoft.kiota.abstractions/1.7.2/lib/net5.0/Microsoft.Kiota.Abstractions.dll : warning IL2104: Assembly 'Microsoft.Kiota.Abstractions' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries [/home/runner/work/profanity-filter/profanity-filter/src/ProfanityFilter.Action/ProfanityFilter.Action.csproj]
/home/runner/.nuget/packages/microsoft.kiota.abstractions/1.7.2/lib/net5.0/Microsoft.Kiota.Abstractions.dll : warning IL3053: Assembly 'Microsoft.Kiota.Abstractions' produced AOT analysis warnings. [/home/runner/work/profanity-filter/profanity-filter/src/ProfanityFilter.Action/ProfanityFilter.Action.csproj]
/home/runner/.nuget/packages/octokit.net.sdk/0.0.4/lib/net8.0/Octokit.NET.SDK.dll : warning IL3053: Assembly 'Octokit.NET.SDK' produced AOT analysis warnings. [/home/runner/work/profanity-filter/profanity-filter/src/ProfanityFilter.Action/ProfanityFilter.Action.csproj]
/home/runner/.nuget/packages/microsoft.kiota.serialization.form/1.1.1/lib/net5.0/Microsoft.Kiota.Serialization.Form.dll : warning IL3053: Assembly 'Microsoft.Kiota.Serialization.Form' produced AOT analysis warnings. [/home/runner/work/profanity-filter/profanity-filter/src/ProfanityFilter.Action/ProfanityFilter.Action.csproj]
/home/runner/.nuget/packages/microsoft.kiota.serialization.json/1.1.2/lib/net5.0/Microsoft.Kiota.Serialization.Json.dll : warning IL3053: Assembly 'Microsoft.Kiota.Serialization.Json' produced AOT analysis warnings. [/home/runner/work/profanity-filter/profanity-filter/src/ProfanityFilter.Action/ProfanityFilter.Action.csproj]
/home/runner/.nuget/packages/microsoft.kiota.serialization.json/1.1.2/lib/net5.0/Microsoft.Kiota.Serialization.Json.dll : warning IL2104: Assembly 'Microsoft.Kiota.Serialization.Json' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries

@eerhardt
Copy link
Member

Here are the detailed warnings I see:

ILC : Trim analysis warning IL2091: Microsoft.Kiota.Abstractions.RequestInformation.Configure<T>(Action`1<RequestConfiguration`1<!!0>>): 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in 'Microsoft.Kiota.Abstractions.RequestInformation.AddQueryParameters<T>(!!0)'. The generic parameter 'T' of 'Microsoft.Kiota.Abstractions.RequestInformation.Configure<T>(Action`1<RequestConfiguration`1<!!0>>)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.  
ILC : AOT analysis warning IL3050: Microsoft.Kiota.Abstractions.RequestInformation.ReplaceEnumValueByStringRepresentation(Object): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: Microsoft.Kiota.Abstractions.RequestInformation.ReplaceEnumValueByStringRepresentation(Object): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : Trim analysis warning IL2075: Microsoft.Kiota.Abstractions.RequestInformation.GetEnumName<T>(!!0): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.PublicNestedTypes', 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.PublicEvents' in call to 'System.Type.GetMember(String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.  
ILC : AOT analysis warning IL3050: GitHub.Repos.Item.Item.Issues.Item.WithIssue_numberPatchRequestBody.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Repos.Item.Item.Issues.Item.WithIssue_numberPatchRequestBody.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Repos.Item.Item.Pulls.Item.WithPull_numberPatchRequestBody.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Repos.Item.Item.Issues.Item.Reactions.ReactionsPostRequestBody.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.PullRequest.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.PullRequest.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Issue.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Issue.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Reaction.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.IssueComment.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: Microsoft.Kiota.Serialization.Form.FormSerializationWriter.WriteEnumValue<T>(String,Nullable`1<!!0>): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: Microsoft.Kiota.Serialization.Json.JsonSerializationWriter.WriteEnumValue<T>(String,Nullable`1<!!0>): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.NullableMilestone.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.AutoMerge.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository_template_repository.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository_template_repository.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository_template_repository.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository_template_repository.Serialize(ISerializationWriter): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : Trim analysis warning IL2075: Microsoft.Kiota.Serialization.Json.JsonSerializationWriter.WriteNonParsableObjectValue<T>(String,!!0): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository_template_repository.<GetFieldDeserializers>b__354_50(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository_template_repository.<GetFieldDeserializers>b__354_51(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository_template_repository.<GetFieldDeserializers>b__354_67(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository_template_repository.<GetFieldDeserializers>b__354_68(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.NullableMilestone.<GetFieldDeserializers>b__70_12(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository.<GetFieldDeserializers>b__402_56(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository.<GetFieldDeserializers>b__402_57(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository.<GetFieldDeserializers>b__402_75(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Repository.<GetFieldDeserializers>b__402_76(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.AutoMerge.<GetFieldDeserializers>b__22_3(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.IssueComment.<GetFieldDeserializers>b__62_0(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Reaction.<GetFieldDeserializers>b__26_0(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Issue.<GetFieldDeserializers>b__142_3(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.Issue.<GetFieldDeserializers>b__142_28(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.PullRequest.<GetFieldDeserializers>b__198_4(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Models.PullRequest.<GetFieldDeserializers>b__198_42(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Repos.Item.Item.Issues.Item.Reactions.ReactionsPostRequestBody.<GetFieldDeserializers>b__10_0(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Repos.Item.Item.Pulls.Item.WithPull_numberPatchRequestBody.<GetFieldDeserializers>b__26_3(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Repos.Item.Item.Issues.Item.WithIssue_numberPatchRequestBody.<GetFieldDeserializers>b__38_5(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  
ILC : AOT analysis warning IL3050: GitHub.Repos.Item.Item.Issues.Item.WithIssue_numberPatchRequestBody.<GetFieldDeserializers>b__38_6(IParseNode): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.  

@IEvangelist
Copy link
Member Author

It looks like most (if not all) of them are related to System.Enum.GetValues(Type). Is that easy to fix, do we already have a mechanism in place for enum support that doesn't require reflection?

@IEvangelist
Copy link
Member Author

I went digging a bit, and found that this bit of code in the Microsoft.Kiota.Abstractions library, it seems to be correct:

Microsoft.Kiota.Abstractions

@baywet
Copy link
Member

baywet commented Jan 29, 2024

Hi everyone,
Thanks for using kiota and for reaching out.
I'm not sure why it's complaining about a reference to Enum.GetValues since it's not used in RequestInformation.
For sanity, can you try to force the Microsoft.Kiota.Abstractions dependency to its latest version (1.7.6) and see if you still see the first warning please?

@baywet
Copy link
Member

baywet commented Jan 29, 2024

As per the warnings in Json/Form serialization, most of them are due to this (and equivalent in form)
I thought the point of adding the annotations/attributes was so the compiler knows not to trim things?

@baywet baywet self-assigned this Jan 29, 2024
@baywet baywet added question Csharp Pull requests that update .net code labels Jan 29, 2024
@baywet baywet added this to the Backlog milestone Jan 29, 2024
@eerhardt
Copy link
Member

The Enum.GetValues(Type) warning is the same as the case study in https://devblogs.microsoft.com/dotnet/creating-aot-compatible-libraries/#dotnet-extensions, the last example in that section.

Lastly, some code was inspecting all the values of an enum. In earlier versions of .NET, the way to do this was to call Enum.GetValues(typeof(MyEnum)). However, this API isn’t compatible with AOT because an array of MyEnum needs to be created at runtime, and the AOT code might not contain the specific code for MyEnum[].


I went digging a bit, and found that this bit of code in the Microsoft.Kiota.Abstractions library, it seems to be correct:
Microsoft.Kiota.Abstractions

The issue here is that the annotations are for the T type, but the actual code doing Reflection is working on the var type = value.GetType();. Usually, these can be different types because of inheritance.

public class MyBase {}
public class MyDerived : MyBase {}

private void Foo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(T value)
{
    var type = value.GetType();
}

MyBase b = new MyDerived();
Foo(b);

In the above example T == typeof(MyBase), but var type == typeof(MyDerived). So that's why there is a warning here - you can't statically know which type value will be at runtime. The fix there is to use var type = typeof(T); instead.

(Side note, since this actual code has where T : Enum you could argue that the tools should be smart enough that T and value.GetType() will always be equal, since enums can't participate in inheritance.)

@baywet
Copy link
Member

baywet commented Jan 29, 2024

Thanks for the details here!
@IEvangelist do you want to start with a PR to this replacing the GetType by a typeof instead?

@IEvangelist
Copy link
Member Author

Thanks for the details here! @IEvangelist do you want to start with a PR to this replacing the GetType by a typeof instead?

Sure, @baywet - see microsoft/kiota-dotnet#184

@baywet baywet modified the milestones: Backlog, Kiota v1.11 Feb 1, 2024
@baywet baywet moved this from Todo to In Progress in Kiota Feb 1, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in Kiota Feb 1, 2024
@baywet
Copy link
Member

baywet commented Feb 1, 2024

Update for everyone: with the help @IEvangelist and others we've fixed the code across the multiple kiota libraries. This will get closed as the last PR gets merged. Feel free to comment on this issue if you're still seeing warnings.

@IEvangelist
Copy link
Member Author

FYI @nickfloyd

@iPaul-777
Copy link

@IEvangelist's answer works but not useful in some instances. If you know the type, typeof(mytype) does, indeed, work. But, with non-native code, it also accepts object.GetType() so the object can be anything and it just works. If you want to continue handling all types with native AOT, you now have to have a big switch statement with all of your types, using typeof(type) for each. Is there any generic solution??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Csharp Pull requests that update .net code Needs: Attention 👋
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants