Код: Выделить всё
void Method(T arg) where T : allows ref struct
{
// Cannot implicitly convert type 'T' to 'object'
throw new ArgumentException(arg.ToString());
// Argument 1: cannot convert from 'T' to 'bool'
throw new ArgumentException(Convert.ToString(arg));
// The type 'T' may not be a ref struct or a type parameter allowing ref structs
// in order to use it as parameter 'T' in the generic type
// or method 'DefaultInterpolatedStringHandler.AppendFormatted(T)'
throw new ArgumentException($"{arg}");
}
Есть ли какой-нибудь трюк, чтобы сделать это возможным?
Обновление: Я был бы удовлетворен, если бы мог найти способ отображать значение ограниченного набора типов T, например string:
Код: Выделить всё
void Method(T arg) where T : allows ref struct
{
// An expression of type 'T' cannot be handled by a pattern of type 'string'.
if (arg is string s) throw new ArgumentException(s);
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... c-argument
Мобильная версия