dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.58k stars 10.06k forks source link

Blazor IJSInProcessObjectReference.Invoke is attributed with RequiresUnreferencedCode #58712

Open yueyinqiu opened 1 month ago

yueyinqiu commented 1 month ago

Hello, I have recently found that IJSInProcessObjectReference.Invoke<TValue> is attributed under [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")] here.

However we have add [DynamicallyAccessedMembers(JsonSerialized)] for its TValue, so it seems unnecessary to use RequiresUnreferencedCode?

And we didn't do that in IJSObjectReference, so I believe it should be a mistake.

javiercn commented 1 month ago

@yueyinqiu thanks for contacting us.

I'm not sure what the right thing to do here is. From what I know, I believe that both are required. The challenge with DynamicallyAccessedMembers is that it only "preserves" the first level, and not the entire "object graph".

You could say that DynamicallyAccessedMembers is then not enough to tell the linker what it needs to preserve, and it also only affects the return value itself, and not any other parameter that gets passed in.

RequiresUnreferencedCode provides a broader warning that when calling that API, you might need to ensure other types are preserved.

Based on that, I think the right thing to do would be to annotate the other APIs with [RequiresUnreferencedCode] too. I'm not sure why it wasn't done.