Skip to content

Conversation

neuecc
Copy link
Contributor

@neuecc neuecc commented Sep 19, 2025

Currently, when specifying abstract properties or methods with ParamsSource, an exception occurs with SingleOrDefault:
System.InvalidOperationException: Sequence contains more than one matching element

BenchmarkRunner.Run<Bench>();

public abstract class BenchBase
{
    public abstract int[] Levels { get; }

    [ParamsSource(nameof(Levels))]
    public int Level { get; set; }

    [Benchmark]
    public void Foo()
    {
    }
}

public class Bench : BenchBase
{
    public override int[] Levels => [1, 2, 3];

    [Benchmark]
    public void Bar()
    {
    }
}

This is because sourceType.GetAllMethods() and sourceType.GetAllProperties() traverse up the parent class hierarchy, enumerating both the overridden property and abstract property with the same name.
And since GetValidValuesForParamsSource processes with SingleOrDefault, the exception occurs.

Since the enumeration starts from sourceType in order, this should be safe to change to FirstOrDefault as the best match.

@timcassell
Copy link
Collaborator

Can you add an integration test for that?

@neuecc
Copy link
Contributor Author

neuecc commented Sep 19, 2025

Thank you for checking quickly!
I've added tests.

Copy link
Collaborator

@timcassell timcassell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @neuecc!

@timcassell timcassell merged commit 6a47e26 into dotnet:master Sep 19, 2025
9 checks passed
@timcassell timcassell added this to the v0.15.4 milestone Sep 19, 2025
This was referenced Oct 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants