2012-06-21 4 views
5

System.Reflection 및 System.Reflection.Emit에 의해 IL에서 if-else를 처리하려고합니다. 개체의 인스턴스로 설정되지 않았습니다C# 다른 예외가있는 경우

개체 참조 :

Label inequality = new System.Reflection.Emit.Label(); 
Label equality = new System.Reflection.Emit.Label(); 
Label end = new System.Reflection.Emit.Label(); 
var method = new DynamicMethod("dummy", null, Type.EmptyTypes); 
var g = method.GetILGenerator(); 
g.Emit(OpCodes.Ldstr, "string"); 
g.Emit(OpCodes.Ldstr, "string"); 
g.Emit(OpCodes.Call, typeof(String).GetMethod("op_Equality", new Type[]{typeof(string), typeof(string)})); 
g.Emit(OpCodes.Ldc_I4, 0); 
g.Emit(OpCodes.Ceq); 
g.Emit(OpCodes.Brtrue_S, inequality); 
g.MarkLabel(inequality); //HERE it throws exception 
g.Emit(OpCodes.Ldstr, "Specified strings are different."); 
g.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[]{typeof(string)})); 
g.Emit(OpCodes.Br_S, end); 
g.Emit(OpCodes.Brfalse_S, equality); 
g.MarkLabel(equality); 
g.Emit(OpCodes.Ldstr, "Specified strings are same."); 
g.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) })); 
g.Emit(OpCodes.Br_S, end); 
g.MarkLabel(end); 
g.Emit(OpCodes.Ret); 

var action = (Action)method.CreateDelegate(typeof(Action)); 
action(); 

Console.Read(); 

지금, 라벨을 표시하고있어 행에 나에게이 예외를 throw : 이것은 내가 현재 가지고있는 코드입니다.

My exception.

는하지만 그 레이블이 새 레이블 개체와 연결되어 있기 때문에 그것은 어리 석음을 생각합니다. 이 문제를 어떻게 해결할 수 있습니까? 감사.

답변

7

g을 정의한 후에 레이블을 Label whatever = g.DefineLabel();으로 정의하지 않아도됩니까?

+0

아, 맞습니다. 고맙습니다. – user35443