dav*_*ser 2 c# casting exception
为什么我InvalidCastException试图这样做?
throw (ArgumentNullException)(new Exception("errormessage", null));
Run Code Online (Sandbox Code Playgroud)
这是以下功能的简化版本.
public static void Require<T>(bool assertion, string message, Exception innerException) where T: Exception
{
if (!assertion)
{
throw (T)(new Exception(message, innerException));
}
}
Run Code Online (Sandbox Code Playgroud)
完整的错误消息是:
System.InvalidCastException:无法将类型为"System.Exception"的对象强制转换为"System.ArgumentNullException".
我的印象是您正在实例化一个基类并尝试将其转换为派生类.我不认为你能做到这一点,因为Exception比ArgumentNullException更"通用".你可以反过来做到这一点.