コード例 #1
0
        private void Obfuscate(ILInstruction instruction)
        {
            if (instruction.OpCode != OpCodes.Ldstr)
            {
                return;
            }

            int index = AddString((string)instruction.Value);

            instruction.OpCode = OpCodes.Ldsfld;
            instruction.Value  = _stringFieldRef;

            var next = new ILInstruction(Instruction.GetLdc(index));

            instruction.AddNext(next);
            instruction = next;

            next = new ILInstruction(OpCodes.Ldelem_Ref);
            instruction.AddNext(next);
        }
        private bool Build(ILInstruction instruction)
        {
            var methodRef = instruction.Value as MethodReference;

            if (methodRef == null)
            {
                return(false);
            }

            var opCode = instruction.OpCode;

            if (opCode != OpCodes.Call && opCode != OpCodes.Callvirt)
            {
                return(false);
            }

            if (methodRef.HasThis ||
                methodRef.CallConv != MethodCallingConvention.Default)
            {
                return(false);
            }

            int methodIndex;

            _methodRefs.TryAdd(methodRef, out methodIndex);

            instruction.OpCode = OpCodes.Ldsfld;
            instruction.Value  =
                new FieldReference(
                    "MethodPointers",
                    new ArrayType(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.IntPtr, _module.Assembly)),
                    new TypeReference(_mainType.Name, _mainType.Namespace, false));
            instruction = instruction.AddNext(Instruction.GetLdc(methodIndex));
            instruction = instruction.AddNext(OpCodes.Ldelem_I);
            instruction = instruction.AddNext(OpCodes.Calli, methodRef.CallSite);

            return(true);
        }