技术开发 频道

C#中泛型的一点遗憾

   然而,我总感到非常遗憾,既然泛型的where约束中可以使用new(),那么,为何不能提供带参的构造函数约束呢?例如这样的实现:

public static class ThrowHelperGeneric<TCustomException>
    where TCustomException : ApplicationException,
new(), new(string), new(string, Exception)
{
    
public static TCustomException ThrowCustomException(string message)
    {
        LogService.Error(message);
        throw
new TCustomException(message);
    }

    
public static TCustomException ThrowCustomException(string message, Exception innerException)
    {
        LogService.Error(message, innerException);
        throw
new TCustomException(message, innerException);
    }
}

   不知在将来的C#版本中可否提供这样的泛型支持呢?莫非,实现这样的语法还存在相当的难度?

0
相关文章