Skip to content

Commit 9c7a2e0

Browse files
committed
Fix NullReferenceException when GetMethod is null for a property
1 parent a2871df commit 9c7a2e0

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/BenchmarkDotNet/Running/BenchmarkConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ private static (MemberInfo source, object[] values) GetValidValuesForParamsSourc
321321
paramsSourceMethod,
322322
sourceType));
323323

324-
var paramsSourceProperty = sourceType.GetAllProperties().FirstOrDefault(property => property.Name == sourceName && property.GetMethod.IsPublic);
324+
var paramsSourceProperty = sourceType.GetAllProperties().FirstOrDefault(property => property.Name == sourceName && property.GetMethod?.IsPublic == true);
325325

326326
if (paramsSourceProperty != default)
327327
return (paramsSourceProperty, ToArray(

tests/BenchmarkDotNet.Tests/ParamsSourceTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,32 @@ public static IEnumerable<object> Values()
3131
[Benchmark]
3232
public object FooBar() => O;
3333
}
34+
35+
// #2980
36+
[Fact]
37+
public void WriteOnlyPropertyDoesThrowNullReferenceException()
38+
{
39+
var exception = Assert.Throws<InvalidBenchmarkDeclarationException>(
40+
() => BenchmarkConverter.TypeToBenchmarks(typeof(ClassWithWriteOnlyProperty)));
41+
42+
Assert.Contains(nameof(ClassWithWriteOnlyProperty.WriteOnlyValues), exception.Message);
43+
Assert.Contains("no public, accessible method/property", exception.Message);
44+
}
45+
46+
public class ClassWithWriteOnlyProperty
47+
{
48+
private int _writeOnlyValue;
49+
50+
public int WriteOnlyValues
51+
{
52+
set { _writeOnlyValue = value; }
53+
}
54+
55+
[ParamsSource(nameof(WriteOnlyValues))]
56+
public int MyParam { get; set; }
57+
58+
[Benchmark]
59+
public void Run() { }
60+
}
3461
}
3562
}

0 commit comments

Comments
 (0)