jbevain / cecil

Cecil is a library to inspect, modify and create .NET programs and libraries.
MIT License
2.74k stars 624 forks source link

Add the ability to create Instructions without the appropriate operand type #932

Open slxdy opened 9 months ago

slxdy commented 9 months ago

Because this isn't currently possible, cloning instructions or pre-making instructions before assigning their operator is hell. It's an unnecessary limitation that simply shouldn't exist.

slxdy commented 9 months ago

My current workaround (not really safe):

public static Instruction CreateInstructionRaw(OpCode opCode, object? operand = null)
{
    var instruction = (Instruction)RuntimeHelpers.GetUninitializedObject(typeof(Instruction));
    instruction.OpCode = opCode;
    instruction.Operand = operand;
    return instruction;
}