메서드에 전달 된 예외 형식 매개 변수를 기반으로 예외를 throw하려고합니다.형식 매개 변수를 기반으로 새 개체 인스턴스화
public void ThrowException<T>(string message = "") where T : SystemException, new()
{
if (ConditionMet)
{
if(typeof(T) is NullReferenceException)
throw new NullReferenceException(message);
if (typeof(T) is FileNotFoundException)
throw new FileNotFoundException(message);
throw new SystemException(message);
}
}
가 이상적으로 내가 SystemException
의 기본 형식은 내가 가진 것 가지고 주어진 new T(message)
그런 짓을 할 : 여기
이 할 수없는,하지만 해결 방법이 있습니다. 여기를 참고하십시오 : http://stackoverflow.com/questions/7772414/can-i-use-generic-constraints-to-enable-a-parameterized-constructor/7772426#7772426 –
또한, 사용자 코드에서 NullReferenceException과 같은 예외를 throw합니다. –