ArgumentException при вызове MethodInfo.MakeGenericMethodC#

Место общения программистов C#
Anonymous
ArgumentException при вызове MethodInfo.MakeGenericMethod

Сообщение Anonymous »

Код: Выделить всё

public void Test(T reference, T same, T differentOrGreater)
{
var comparisonInterface = typeof(T)
.GetInterfaces()
.FirstOrDefault(
i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(IComparisonOperators));

this.GetType()
.GetMethod(nameof(this.CheckComparisonOperators),
BindingFlags.NonPublic | BindingFlags.Instance)!
.MakeGenericMethod(typeof(T))
.Invoke(this, [reference, same, differentOrGreater]);
}

private void CheckComparisonOperators(T reference, T same, T greater)
where T : IComparisonOperators
{
// do stuff
}

public record BaseSize(uint Value) : IComparisonOperators
where T : BaseSize
{
// implementation
}

public record DerivedSize(uint Value) : BaseSize(Value);
Вызов отлично работает с BaseSize, но не работает с DerivedSize:

Код: Выделить всё

Test(new DerivedSize(1), new DerivedSize(1), new DerivedSize(2));
как это исправить?


Подробнее здесь: https://stackoverflow.com/questions/798 ... ericmethod

Вернуться в «C#»