-
Notifications
You must be signed in to change notification settings - Fork 127
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
Deduce closed generic type usages based GetGenericTypeDefinition/MakeGenericType #3080
Comments
Just to be sure we are on the same page, what is not working here? |
I assume the question is mostly related to AOT, right? Since the above will work just fine with trimming alone. The The AOT specific ability to track instantiations made via What would help is if you have pointers to existing code with such patterns, so that we can rationalize the importance of having this support. |
Yes. That’s related to AOT, sorry for missing that, I understand that regular trimming unrelated. I will look for real world code. I actually come up with that pattern when try to create presentation about shortcomings of NativeAOT. I will look at EF Core and FSharp for similar patterns. If that’s reasonably easy, I would like to implement this, since that allow me more confidently have a message that unless you are doing some really arcane meta programming you are in loving hands of NativeAOT |
If you want to give a try - definitely go for it. I'll try to help as much as I can. |
I'd like to see the real-world use cases for this - suggestions for this kind of analysis do come up (dotnet/runtime#81204), and it can be analyzed, but if all the type parameters are known, it can be rewritten to not use reflection at all (like in your example). That's a much better fix and less analyzer code to maintain. |
Thanks @MichalStrehovsky for pointing out that this would only work in very limited situations. Basically only if the types are statically known - in which case rewriting it without reflection is definitely the better approach. The only scenario which might be slightly useful and doesn't require statically known types is something like this: void TestMethod([DynamicallyAccessedMembers(PublicParameterlessConstructor)] Type type, Type elementType)
{
var genericType = type.GetGenericTypeDefinition();
var newType = genericType.MakeGenericType(elementType); // Warns - since it can't tell if the generic type parameters are annotated
var inst = Activator.CreateInstance(newType); // Currently this will warn, if we recognized GetGenericTypeDefinition we could make this not warn in this case.
} It's questionable how much value there is in supporting the above. |
How complicated would be to teach linker that
GetGenericTypeDefinition
produce generic type definition for well-known type, and thatMakeGenericType
produce well known specialized type.and if complicated, why it's complicated?
The text was updated successfully, but these errors were encountered: