2011-07-17 2 views
1

유형으로부터 프록시를 구축하는 코드가 있습니다. 그것은 완벽하게 작동합니다. 다음 setter emit 코드를 추가하면 호출 할 때 isDirty 비트를 푸시해야합니다. 실패, 왜?런타임에 방출 코드가 작동하지만 디버그에 충돌하지 않습니다. 이유는 무엇입니까

isDirty 비트없이 코드를 실행하면 작동합니다. isDirty 비트를 사용하여 코드를 실행하면 디버그에서 작동하지만 Visual Studio에서 디스 어셈블리 창을 시작합니다. isDirty (디버그하지 않음) 코드를 실행하면 프로그램이 중단되고 (응답하지 않음) 취소 버튼을 누르면 작업이 시작되고 모든 rigth 데이터가 표시됩니다.

 PropertyBuilder property = proxy.DefineProperty(propertyInfo.Name, propertyInfo.Attributes, propertyInfo.PropertyType, null); 
     MethodAttributes attributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Virtual; 
     MethodBuilder setMethod = proxy.DefineMethod("set_" + propertyInfo.Name, attributes, typeof(void), new Type[] { propertyInfo.PropertyType }); 

     ILGenerator setIL = setMethod.GetILGenerator(); 
     setIL.Emit(OpCodes.Ldarg_0); // load this on the stack - where this is the type we are creating 
     setIL.Emit(OpCodes.Ldarg_1); // load first parameter on the stack 
     setIL.Emit(OpCodes.Call, propertyInfo.GetSetMethod()); 

     { 
     //error here: when this is uncomment, it fails 
     // // set the isDirty bit 
     // setIL.Emit(OpCodes.Ldarg_0); // load this on the stack 
     // setIL.Emit(OpCodes.Ldc_I4_1, 1); // push a number on the stack 
     // setIL.Emit(OpCodes.Stfld, isDirtyField); // save the value on the stack in field 
     } 

     setIL.Emit(OpCodes.Ret); 

     property.SetSetMethod(setMethod); 

왜 이것이 실패하는지 보는 데 어려움이 있습니까? 전문가의 도움이 필요하십니까 :)

// 데니스

답변

1

을이 유일한 문제가 있는지 잘 모르겠어요,하지만 Ldc_I4_1 오피 코드를 방출 할 때 잘못된 Emit 오버로드를 사용하고 있습니다. 당신이 중 하나를 수행해야합니다

setIL.Emit(OpCodes.Ldc_I4_1) 

또는

setIL.Emit(OpCodes.Ldc_I4, 1) 

는 전문 오피 코드를 사용하기 때문에 두 번째가되는 숫자에 국한되지 반면 첫 번째 옵션은, 약간 작은 IL 방법 본체가 발생합니다 짐을 실은.

+0

나는 무엇을하고 있었는지 보았다 : 방출에 대해 말해서, 나는 그것을 스택에 값 하나를 밀어 넣기 만했지만 값을 제공했다. 또는 그렇게 생각합니다. 하지만 작동합니다. 고마워 –

관련 문제