-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Analyzer: Detect the problem of using a System.Type overload instead of the generic overload #68243
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection Issue DetailsRelated to #67444 (comment) Passing a Example: public void M1()
{
// Detect:
int size = Marshal.SizeOf(typeof(SampleType));
// Suggest fix:
int size = Marshal.SizeOf<SampleType>());
} Suggested Category: Reliability
|
We run a dataflow analysis in the compiler for the purposes of reflection so We already have a Roslyn analyzer that warns for this - the We could add a new analyzer just for this that is enabled by default (because the fix is really simple and everyone should just do it), but it duplicates part of what the existing analyzer already does. The ideal fix is really not to introduce new APIs that requires this kind of special casing... Cc @tlakollo |
From your comment
I was guessing there is a general purpose linker analyzer for this, though I believe this analyzer will be quite different from that one. It will only flag if there is an generic overload with matching parameter count exist and only if the type is created using the CC @eerhardt |
It limits the damage, but it's not great (instead of a runtime exception in a random nuget one cannot debug into, one gets a compile-time warning in the same random nuget that one cannot fix anyway because it's how nuget works). If we have an analyzer that always flags it, I'm fine with that, just noting the duplication. |
Current analyzer delivered with the
This new analyzer would be different in almost all the aspects
|
Completely agree, thank you for explaining how that ILLink analyzer works, good to know |
Looks good as proposed, that is handling any method, not just Category: Usage |
Estimates:
|
Per triage @buyaa-n moving to Future |
Would be happy to tackle this one next! |
Related to #67444 (comment)
Passing a
System.Type
as a parameter is not AOT friendly, when APIs offers overloads with runtimeType
parameter and generic type parameter, the former should only used when the type is not known at compile time. But it happens that people use the runtimeType
overload usingtypeof
operator when the type is known, using the generic overload in this case will be AOT friendlyExample:
Suggested Category: Usage
Suggested Severity: info
CC @carlossanlop
The text was updated successfully, but these errors were encountered: