Closed
Description
Consider this example. I n my understanding that's perfectly valid annotations. What am I missing?
using System.Diagnostics.CodeAnalysis;
var validationParameters = new ValidationParameters();
foreach (var typeGroup in validationParameters.Benchmarks.GroupBy(benchmark => benchmark.Descriptor.Type))
{
// warning IL2072: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'TryCreateBenchmarkTypeInstance(Type, out Object)'. The return value of method 'System.Linq.IGrouping<TKey, TElement>.Key.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
if (!TryCreateBenchmarkTypeInstance(typeGroup.Key, out var benchmarkTypeInstance))
{
continue;
}
}
bool TryCreateBenchmarkTypeInstance(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
Type type, out object? instance)
{
try
{
instance = Activator.CreateInstance(type);
return true;
}
catch (Exception)
{
instance = null;
return false;
}
}
public class ValidationParameters
{
public IReadOnlyList<BenchmarkCase> Benchmarks { get; } = new List<BenchmarkCase>();
}
public class BenchmarkCase
{
public Descriptor Descriptor { get; } = new Descriptor();
}
public class Descriptor
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
public Type Type { get; set; } = null!;
}
This is simplified version of https://github.com/dotnet/BenchmarkDotNet/blob/5e62756c126980ecde9e4779b3c320b3225381ee/src/BenchmarkDotNet/Validators/ExecutionValidatorBase.cs#L25-L27 in my naive attempt to address Michal's suggestion dotnet/BenchmarkDotNet#2020 (comment)
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status
No status