Git Product home page Git Product logo

Comments (8)

jnm2 avatar jnm2 commented on June 13, 2024 1

Though if we only do what the user asks, maybe skip the BindingFlags and let the other fix handle that:

typeof(Foo).GetMethod(nameof(this.Id), new[] { typeof(int) });

from reflectionanalyzers.

JohanLarsson avatar JohanLarsson commented on June 13, 2024

What do you want the call to look like here:

Before:

class Foo
{
    public Foo()
    {
        var methodInfo = typeof(Foo).GetMethod(nameof(this.Id));
    }

    public int Id(int value) => value;
}

from reflectionanalyzers.

jnm2 avatar jnm2 commented on June 13, 2024
class Foo
{
    public Foo()
    {
        var methodInfo = typeof(Foo).GetMethod(nameof(this.Id), BindingFlags.Instance | BindingFlags.Public 😇, null, new[] { typeof(int) }, null);
    }

    public int Id(int value) => value;
}

from reflectionanalyzers.

JohanLarsson avatar JohanLarsson commented on June 13, 2024

I'm leaning towards the latter, fix adding as little as possible. Thanks for the code!
We can offer two fixes though, simple.

from reflectionanalyzers.

JohanLarsson avatar JohanLarsson commented on June 13, 2024

By properties do you mean indexers?

from reflectionanalyzers.

JohanLarsson avatar JohanLarsson commented on June 13, 2024

About constructors there is no overload without Type[]

from reflectionanalyzers.

jnm2 avatar jnm2 commented on June 13, 2024

@JohanLarsson

By properties do you mean indexers?

Yes, including named indexers in assemblies written in other languages.

Example 1

void Main()
{
    // AmbiguousMatchException
    typeof(C).GetProperty(
        "Item",
        BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Dump();
}

class C
{
    public int this[int p1] => 0;
    public int this[int p1, int p2] => 0;
}

Analyzer should flag as error and offer two fixes:

typeof(C).GetProperty(
    "Item",
    BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
    null,
    typeof(int),
    new[] { typeof(int) },
    null)
typeof(C).GetProperty(
    "Item",
    BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
    null,
    typeof(int),
    new[] { typeof(int), typeof(int) },
    null)

Example 2

void Main()
{
    // AmbiguousMatchException
    typeof(C).GetProperty(
        "Foo",
        BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Dump();
}

class C
{
    [IndexerName("Foo")]
    public int this[int p1] => 0;

    [IndexerName("Foo")]
    public int this[int p1, int p2] => 0;
}

Analyzer should flag as error and offer two fixes:

typeof(C).GetProperty(
    "Foo",
    BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
    null,
    typeof(int),
    new[] { typeof(int) },
    null)
typeof(C).GetProperty(
    "Foo",
    BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
    null,
    typeof(int),
    new[] { typeof(int), typeof(int) },
    null)

Example 3

void Main()
{
    // AmbiguousMatchException
    typeof(C).GetProperty(
        "Foo",
        BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Dump();
}
Class C
    ' Not an indexed property
    Public ReadOnly Property Foo As Integer
        Get
            Return 0
        End Get
    End Property
    
    ' Indexed property (not Default, so must be named: Me.Foo(i) instead of Me(i))
    Public ReadOnly Property Foo(ByVal p1 As Integer) As Integer
        Get
            Return 0
        End Get
    End Property
End Class

Analyzer should flag as error and offer two fixes:

typeof(C).GetProperty(
    "Foo",
    BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
    null,
    typeof(int),
    Type.EmptyTypes,
    null)
typeof(C).GetProperty(
    "Foo",
    BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
    null,
    typeof(int),
    new[] { typeof(int) },
    null)

from reflectionanalyzers.

jnm2 avatar jnm2 commented on June 13, 2024

Reopening, but feel free to close and split to a new issue.

from reflectionanalyzers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.