닷넷 사용자 정의 예외 클래스의 최소 구현 코드 - 두 번째 이야기
예전 글에서,
닷넷 사용자 정의 예외 클래스의 최소 구현 코드
; https://www.sysnet.pe.kr/2/0/1580
예외 클래스에 대한 최소 코드를 보여드렸는데요.
[Serializable]
public class RuntimeException : Exception, ISerializable
{
public RuntimeException()
{
}
public RuntimeException(string message)
: base(message)
{
}
public RuntimeException(string message, Exception innerException)
: base(message, innerException)
{
}
protected RuntimeException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
}
.NET 4.0의 보안이 적용되면서 ^^ 한 가지 늘었습니다. Exception 클래스의 GetObjectData 메서드가 다음과 같이 정의되어 있는데,
[SecurityCritical]
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
// ...[생략]...
}
이 때문에 .NET CLR4 보안 모델이 활성화되는 영역에서는 문제가 발생할 수 있습니다.
.NET CLR4 보안 모델 - 1. "Security Level 2"란?
; https://www.sysnet.pe.kr/2/0/1680
예를 들어, 다음과 같이 예외가 발생합니다.
System.TypeLoadException was caught
HResult=-2146233054
Message=Inheritance security rules violated while overriding member: 'My.RuntimeException.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.
Source=TestModule
TypeName=My.RuntimeException.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)
StackTrace:
at Test.GetString()
at Test.Do()
InnerException:
따라서, .NET 4.0부터는 이렇게 예외 클래스를 정의해 주시면 됩니다.
[Serializable]
public class RuntimeException : Exception, ISerializable
{
public RuntimeException()
{
}
public RuntimeException(string message)
: base(message)
{
}
public RuntimeException(string message, Exception innerException)
: base(message, innerException)
{
}
protected RuntimeException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
[SecurityCritical]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
}
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]