C# - Reflection으로 변경할 수 없는 readonly 정적 필드
비주얼 스튜디오에서 그동안 잘 되던 단위 테스트(Unit Test)가 실패하기 시작했습니다. 그러니까, 예를 들어 다음과 같은 식으로 짜여진 프로그램이었습니다.
using System;
using System.Reflection;
namespace ConsoleApp1
{
class Program
{
static int Number = 5;
static void Main(string[] args)
{
Console.WriteLine(Program.Number); // 출력: 5
}
}
}
그런데 단위 테스트를 위해 Number 값을 임시 변경해야 할 필요가 있어서 private static 멤버라 Reflection을 이용해 다음과 같이 변경하는 코드를 추가했고,
class Program
{
static int Number = 5;
static void Main(string[] args)
{
SetStaticField(typeof(Program), "Number", 3);
Console.WriteLine(Program.Number); // 출력: 3
}
public static object SetStaticField(Type type, string fieldName, object newValue)
{
FieldInfo fi = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Static);
object oldValue = fi.GetValue(null);
fi.SetValue(null, newValue);
return oldValue;
}
}
그러다 요즘 들어 비주얼 스튜디오가,
IDE1006 - Naming rule violation: These words must begin with upper case characters: ...
; https://www.sysnet.pe.kr/2/0/12124
IDE0019 - Use pattern matching
; https://www.sysnet.pe.kr/2/0/12125
정적 분석 기능이 강화되면서 변경하지 않는 필드에 대해 readonly 추천을 하길래 무심코 코드를 변경했더니,
static readonly int Number = 5;
static void Main(string[] args)
{
SetStaticField(typeof(Program), "Number", 3);
Console.WriteLine(Program.Number); // 출력: 5
}
다시 값이 변경되지 않는 현상이 발생했습니다. 재미있는 것은, 디버깅 중 Watch 창으로 확인해 보면,
분명히 "Number" 값은 3이지만, 출력은 여전히 5로 나옵니다.
눈치채셨겠지만, static readonly로 인해 값이 안 바뀐다는 가정으로 JIT 컴파일러는 기계어 코드를 생성 시 다음과 같이 아예 그 필드의 값을 상수값으로 처리합니다.
15: Console.WriteLine(Program.Number);
013A090E B9 05 00 00 00 mov ecx,5
013A0913 E8 B0 0F FE 71 call 733818C8
013A0918 90 nop
정리해 보면, Reflection의 대상이 static readonly인 경우 값이 바뀌지 않다는 것에 주의를 해야 합니다. (그나저나 Debug 모드인데도, 이렇게 훌륭하게 ^^; 최적화를 해주는군요.)
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]