dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.92k stars 4.02k forks source link

CodeLens constructor reference not working with target-typed new across projects #67956

Open vsfeedback opened 1 year ago

vsfeedback commented 1 year ago

This issue has been moved from a ticket on Developer Community.


CodeLens constructor reference not working with target-typed new

If you have a public class with a public constructor in a class library targeting netstandard2.0, CodeLens fails to accurately report any references to that constructor from any call-site using implicit (target-typed) new.

For example: In ClassLibrary1, targeting netstandard2.0:

namespace ClassLibrary1
{
    public class Class1
    {
        public Class1()
        {
        }
    }
}

In ClassLibrary2, targeting net6.0:

using ClassLibrary1;
namespace ClassLibrary2;
public class Class2
{
    static Class1 x = new Class1(); // OK
    static Class1 y = new(); // CodeLens doesn't see this
}

Now, back in Class1, look at the CodeLens indicators on the Class1 constructor. It shows only 1 reference when it should show 2.


Original Comments

Feedback Bot on 4/25/2023, 03:45 AM:

(private comment, text removed)


Original Solutions

(no solutions)

sharwell commented 1 year ago

Note that CodeLens already supports implicit object creation, but it appears to only work for references that occur in the same project as the constructor. The above scenario to reproduce this problem occurs when the definition and reference are in different projects. I was able to reproduce the problem as listed, and also for the more specific case where both projects targeted netstandard2.0.

pharring commented 7 months ago

Please note that this isn't just "CodeLens". It also affects "Find All References".