gpt4 book ai didi

c# - 如何在C#中重写Java[继续<标签>和中断<标签>]?

转载 作者:行者123 更新时间:2023-12-02 08:14:13 27 4
gpt4 key购买 nike

在Java中它是这样写的..当我移植这段代码时...意识到没有这样的东西 break <label>continue <label> .

我知道这些命令没有包含在内,因为在使用带有命令的 goto 时必须有一种更简洁的方法来执行此操作。

但我最终使用了..下面的 C# 代码,有什么方法可以将其重写得更干净吗?

Java 代码

for(JClass c : classes) {
for(JMethod m : c.getMethods()) {
JCode code = m.getCode();
if(code == null)
continue;
label: for(int index = 0; index < code.getExceptionLookupTable().length; index++) {
JException e = code.getExceptionTable().get(index);
for(int index2 = e.getStartIndex(); index2 < e.getEndIndex(); index2++)
if(code.getInstruction(index2).getOpcode() == NEW && ((NEW) code.getInstruction(index2)).getType().equals("java/lang/RuntimeException"))
continue label;
if(e.getCatchTypeClassName().equals("java/lang/RuntimeException")) {
for(int index = e.getHandlerIndex(); index < code.getInstrLength(); index++) {
JInstruction instr = code.getInstruction(index);
if(instr.getOpcode() == ATHROW)
break;
else if(instr instanceof ReturnInstruction)
break label;
}
removeStuff(code, ei--);
}
}
}
}

C# 代码。

foreach(JClass c in classes) {
foreach(JMethod m in c.getMethods()) {
JCode code = m.getCode();
if(code == null)
continue;

for(int index = 0; index < code.getExceptionTable().Length; index++) {
bool continueELoop = false;
bool breakELoop = false;
JException e = code.getExceptionTable().get(index);
for(int index2 = e.getStartIndex(); index2 < e.getEndIndex(); index2++) {
if(code.getInstruction(index2).getOpcode() == JInstructions.NEW && ((NEW) code.getInstruction(index2)).getType().Equals("java/lang/RuntimeException")) {
continueELoop = true;
break;
}
}
if(continueELoop) continue;

if(e.getCatchTypeClassName().Equals("java/lang/RuntimeException")) {
for(int index = e.getHandlerIndex(); index < code.getInstrLength(); index++) {
JInstruction instr = code.getInstruction(index);
if (instr.getOpcode() == JInstructions.ATHROW) {
break;
} else if (isReturnInstruction(instr)) {
breakELoop = true;
break;
}
}
removeStuff(code, ei--);
}
if (breakELoop) break;
}
}
}

您可以看到,当查看 Java 版本然后查看移植的 C# 版本时,干净的感觉消失了。我是否犯了一些错误,导致代码变得更短?或者更好看?感谢您的帮助。

最佳答案

我想,在 C# 中,你一开始就不会编写如此难看的代码。

以下代码被重构为多个方法,并将 LINQ 与虚构的类层次结构结合使用:

IEnumerable<JCode> GetCodes(IEnumerable<JClass> classes)
{
return from @class in classes
from method in @class.Methods
where method.Code != null
select method.Code;
}

IEnumerable<Tuple<JCode, JException>> GetCandidates(IEnumerable<JCode> codes)
{
return from code in codes
from ex in code.ExceptionTable
where !code.Instructions
.Skip(ex.Start)
.Take(ex.End - ex.Start + 1)
.Any(i => i.OpCode == New && ...)
select Tuple.Create(code, ex);
}

然后

void RewriteMethods(IEnumerable<JClass> classes)
{
var codes = GetCodes(classes);

var candidates = GetCandidates(codes);

foreach (var candidate in candidates)
{
var code = candidate.Item1;
var ex = candidate.Item2;

var instructionsToRemove = code.Instructions
.Skip(ex.HandlerStart)
.TakeWhile(i => i.OpCode != Return)
.Where(i => i.OpCode == AThrow);

code.RemoveAll(instructionsToRemove);
}
}

关于c# - 如何在C#中重写Java[继续<标签>和中断<标签>]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6756383/

27 4 0