FxCOp 自定义规则
FxCOp Custom Rules
我一直在研究 FxCOP 自定义规则。我正在努力获取使用的 IL 指令行。我有一个基本的疑问,当我们实现一个 c# 代码时,我们得到操作码,这些操作码是否在每条指令的不同行中(源代码指令在一行中有多个操作码)
现在,我的导师要我计算使用了多个操作码的不同行。但是,据我所知,每条 IL 指令都在不同的行中。我将尝试通过以下方式展示不同意见:
源代码
opcode 1, opcode 2, opcode 3
opcode 4, opcode 5
My Opinion My Mentor's Opinion
Opcode 1 Opcode 1,Opcode 2, Opcode 3
Opcode 2 Opcode 5, Opcode 6
opcode 3
我没能找到哪个是真的。请帮我看看对于任何给定的 c# 代码,IL 指令是否在不同的行中,尽管指令在源代码的同一行中,或者如果这是我的导师所相信的并且有一种方法可以找到不同的行。
谢谢
你可以这样写:
public override ProblemCollection Check(Member member)
{
Method method = member as Method;
if (method == null)
{
return base.Check(member);
}
Console.WriteLine("Method: {0}", member.Name);
InstructionCollection ops = method.Instructions;
foreach (Instruction op in ops)
{
Console.WriteLine("File: {0}, Line: {1}, Pos: {2}, OpCode: {3}", op.SourceContext.FileName, op.SourceContext.StartLine, op.SourceContext.StartColumn, op.OpCode);
}
Console.WriteLine();
return base.Problems;
}
请注意,在 Release 模式下,代码行和 OpCodes 之间的关系有点 "random",因为 C# 编译器可以重新排列代码。在调试模式下,关系更加清晰。
注意 (2) 我只考虑 StartLine
和 StartColumn
。有 EndLine
和 EndColumn
属性。
我一直在研究 FxCOP 自定义规则。我正在努力获取使用的 IL 指令行。我有一个基本的疑问,当我们实现一个 c# 代码时,我们得到操作码,这些操作码是否在每条指令的不同行中(源代码指令在一行中有多个操作码)
现在,我的导师要我计算使用了多个操作码的不同行。但是,据我所知,每条 IL 指令都在不同的行中。我将尝试通过以下方式展示不同意见:
源代码
opcode 1, opcode 2, opcode 3
opcode 4, opcode 5
My Opinion My Mentor's Opinion
Opcode 1 Opcode 1,Opcode 2, Opcode 3
Opcode 2 Opcode 5, Opcode 6
opcode 3
我没能找到哪个是真的。请帮我看看对于任何给定的 c# 代码,IL 指令是否在不同的行中,尽管指令在源代码的同一行中,或者如果这是我的导师所相信的并且有一种方法可以找到不同的行。
谢谢
你可以这样写:
public override ProblemCollection Check(Member member)
{
Method method = member as Method;
if (method == null)
{
return base.Check(member);
}
Console.WriteLine("Method: {0}", member.Name);
InstructionCollection ops = method.Instructions;
foreach (Instruction op in ops)
{
Console.WriteLine("File: {0}, Line: {1}, Pos: {2}, OpCode: {3}", op.SourceContext.FileName, op.SourceContext.StartLine, op.SourceContext.StartColumn, op.OpCode);
}
Console.WriteLine();
return base.Problems;
}
请注意,在 Release 模式下,代码行和 OpCodes 之间的关系有点 "random",因为 C# 编译器可以重新排列代码。在调试模式下,关系更加清晰。
注意 (2) 我只考虑 StartLine
和 StartColumn
。有 EndLine
和 EndColumn
属性。