2011-07-17 51 views
1

我有一些代碼,從一個類型建立一個代理。它工作完美。 然後,我添加了setter發出的代碼,它在調用時必須推入isDirty位。這失敗了,爲什麼?在運行時發出代碼崩潰,但不是在調試,爲什麼

如果我運行的代碼沒有isDirty位,它的工作原理。 如果我用isDirty位運行代碼,它在調試中起作用,但在Visual Studio中啓動反彙編窗口。 如果我用isDirty(without-debug)程序崩潰(沒有響應)運行代碼,但是當我點擊取消時,它開始工作並顯示所有設備數據。

 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

我明白了,我在做的是:告訴散發者,我需要它在堆棧上推送一個值,但也提供了一個值。或者我想。 但它的工作。謝謝 –

相關問題