我使用的是.NET 8,我有这样的界面:
public interface ISortableParams<T>
{
public bool? Descending { get; set; }
public T? OrderBy { get; set; }
}
当我try 这样实现它时:
public enum OrderBy
{
None = 0,
Name = 1,
Email = 2,
}
public class Request : ISortableParams<OrderBy>
{
public bool? Descending { get; set; }
// This property (OrderBy) is not recognized as implementation
// Because it has nullable '?'. If I remove it, it works.
// But the enum might be null.
// This happen because Enum is a class type.
// Are nullable classes not recognized as implicit implementation?
public OrderBy? OrderBy { get; set; }
}
-
我不想显式地实现该接口.
-
加
where T : struct
可以解决这个问题,但我想保持它完全通用,不受限制:
public interface ISortableParams<T>
where T : struct // this makes it work, but I might use a class sometime.
{
public bool? Descending { get; set; }
public T? OrderBy { get; set; }
}
- 如何解决?