我目前面臨着運營商的新問題。使用下面的代碼,我想要使輸出與在C#中使用if ... else
對相同。C#反射:如果... else?
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,);
var action = (Action)method.CreateDelegate(typeof(Action));
action();
Console.Read();
我的問題是:
- 我怎樣才能得到一個指令的地址把它作爲分支操作碼的參數?
BR
和BR_S
,Brtrue
和Brtrue_S
,Brfalse
和Brfalse_S
以及類似說明是否有區別?
感謝。
正如其他人所提到的,分支指令的「_S」版本需要1個字節而不是4個字節的偏移量。如果您知道您的分支將始終位於可用範圍內(-128字節至+127字節),那麼您可以通過使用它們來獲得更緊湊的代碼,但是如果您嘗試使用它們跳轉到外部帶有偏移量的標籤這個範圍,創建委託時會引發異常。 – Iridium