本文整理汇总了C#中Instruction.FindStringInstruction方法的典型用法代码示例。如果您正苦于以下问题:C# Instruction.FindStringInstruction方法的具体用法?C# Instruction.FindStringInstruction怎么用?C# Instruction.FindStringInstruction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Instruction
的用法示例。
在下文中一共展示了Instruction.FindStringInstruction方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessInstruction
void ProcessInstruction(Instruction instruction)
{
var methodReference = instruction.Operand as MethodReference;
if (methodReference == null)
{
return;
}
if (methodReference.DeclaringType.FullName != "Anotar.CommonLogging.LogTo")
{
return;
}
if (!foundUsageInMethod)
{
Method.Body.InitLocals = true;
Method.Body.SimplifyMacros();
}
foundUsageInMethod = true;
FoundUsageInType();
var parameters = methodReference.Parameters;
var instructions = Method.Body.Instructions;
instruction.OpCode = OpCodes.Callvirt;
if (parameters.Count == 0)
{
instructions.Replace(instruction, new[]
{
Instruction.Create(OpCodes.Ldsfld, Field),
Instruction.Create(OpCodes.Ldstr, GetMessagePrefix(instruction)),
Instruction.Create(OpCodes.Ldc_I4_0),
Instruction.Create(OpCodes.Newarr, ModuleWeaver.ModuleDefinition.TypeSystem.Object),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetNormalOperand(methodReference))
});
return;
}
if (methodReference.IsMatch("String", "Exception", "Object[]"))
{
if (messageVar == null)
{
messageVar = new VariableDefinition(ModuleWeaver.ModuleDefinition.TypeSystem.String);
Method.Body.Variables.Add(messageVar);
}
if (exceptionVar == null)
{
exceptionVar = new VariableDefinition(ModuleWeaver.ExceptionType);
Method.Body.Variables.Add(exceptionVar);
}
if (paramsVar == null)
{
paramsVar = new VariableDefinition(ModuleWeaver.ObjectArray);
Method.Body.Variables.Add(paramsVar);
}
instructions.Replace(instruction, new[]
{
Instruction.Create(OpCodes.Stloc, paramsVar),
Instruction.Create(OpCodes.Stloc, exceptionVar),
Instruction.Create(OpCodes.Stloc, messageVar),
Instruction.Create(OpCodes.Ldsfld, Field),
Instruction.Create(OpCodes.Ldstr, GetMessagePrefix(instruction)),
Instruction.Create(OpCodes.Ldloc, messageVar),
Instruction.Create(OpCodes.Call, ModuleWeaver.ConcatMethod),
Instruction.Create(OpCodes.Ldloc, exceptionVar),
Instruction.Create(OpCodes.Ldloc, paramsVar),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetExceptionOperand(methodReference)),
});
return;
}
if (methodReference.IsMatch("String", "Object[]"))
{
var stringInstruction =instruction.FindStringInstruction();
if (stringInstruction != null)
{
var operand = GetMessagePrefix(instruction) + (string) stringInstruction.Operand;
instructions.Replace(stringInstruction, new[]
{
Instruction.Create(OpCodes.Ldsfld, Field),
Instruction.Create(stringInstruction.OpCode, operand),
});
instruction.Operand = ModuleWeaver.GetNormalOperand(methodReference);
return;
}
if (messageVar == null)
{
messageVar = new VariableDefinition(ModuleWeaver.ModuleDefinition.TypeSystem.String);
Method.Body.Variables.Add(messageVar);
}
if (paramsVar == null)
{
paramsVar = new VariableDefinition(ModuleWeaver.ObjectArray);
Method.Body.Variables.Add(paramsVar);
}
instructions.Replace(instruction, new[]
{
Instruction.Create(OpCodes.Stloc, paramsVar),
Instruction.Create(OpCodes.Stloc, messageVar),
//.........这里部分代码省略.........
示例2: HandleStringAndArray
void HandleStringAndArray(Instruction instruction, MethodReference methodReference)
{
var instructions = Method.Body.Instructions;
var stringInstruction = instruction.FindStringInstruction();
if (stringInstruction == null)
{
if (messageVar == null)
{
messageVar = new VariableDefinition(ModuleWeaver.ModuleDefinition.TypeSystem.String);
Method.Body.Variables.Add(messageVar);
}
if (paramsVar == null)
{
paramsVar = new VariableDefinition(ModuleWeaver.ObjectArray);
Method.Body.Variables.Add(paramsVar);
}
var exitNop = Instruction.Create(OpCodes.Nop);
var replacement = new List<Instruction>
{
// store the variables
Instruction.Create(OpCodes.Stloc, paramsVar),
Instruction.Create(OpCodes.Stloc, messageVar),
//Append if
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Instruction.Create(OpCodes.Ldc_I4, ModuleWeaver.GetLevelForMethodName(methodReference)),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.IsEnabledMethod),
Instruction.Create(OpCodes.Brfalse, exitNop)
};
AppendExtraContext(instruction, replacement);
replacement.Append(
//put the variable back on the stack params
Instruction.Create(OpCodes.Ldloc, messageVar),
Instruction.Create(OpCodes.Ldloc, paramsVar),
//call the write method
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetNormalOperand(methodReference)),
exitNop
);
;
instructions.Replace(instruction, replacement);
}
else
{
var exitNop = Instruction.Create(OpCodes.Nop);
var replacement = new List<Instruction>
{
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Instruction.Create(OpCodes.Ldc_I4, ModuleWeaver.GetLevelForMethodName(methodReference)),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.IsEnabledMethod),
Instruction.Create(OpCodes.Brfalse, exitNop)
};
AppendExtraContext(instruction, replacement);
replacement.Append(
//re-write stringInstruction contentes
Instruction.Create(stringInstruction.OpCode, (string) stringInstruction.Operand)
);
instructions.Replace(stringInstruction, replacement);
instruction.Operand = ModuleWeaver.GetNormalOperand(methodReference);
instructions.Insert(instructions.IndexOf(instruction)+1, exitNop);
}
}
示例3: ProcessInstruction
//.........这里部分代码省略.........
{
funcVar = new VariableDefinition(ModuleWeaver.GenericFunc);
Method.Body.Variables.Add(funcVar);
}
if (exceptionVar == null)
{
exceptionVar = new VariableDefinition(ModuleWeaver.ExceptionType);
Method.Body.Variables.Add(exceptionVar);
}
var sectionNop = Instruction.Create(OpCodes.Nop);
instructions.Replace(instruction,
new[]
{
Instruction.Create(OpCodes.Stloc, funcVar),
Instruction.Create(OpCodes.Stloc, exceptionVar),
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Instruction.Create(OpCodes.Callvirt, isEnabledMethod),
Instruction.Create(OpCodes.Brfalse_S, sectionNop),
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Instruction.Create(OpCodes.Ldloc, exceptionVar),
Instruction.Create(OpCodes.Ldstr, messagePrefix),
Instruction.Create(OpCodes.Ldloc, funcVar),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.FuncInvokeMethod),
Instruction.Create(OpCodes.Call, ModuleWeaver.ConcatMethod),
Instruction.Create(OpCodes.Ldnull),
Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetExceptionOperand(methodReference)),
sectionNop
});
return;
}
if (methodReference.IsMatch("String", "Object[]"))
{
var stringInstruction = instruction.FindStringInstruction();
if (stringInstruction != null)
{
var operand = messagePrefix + (string) stringInstruction.Operand;
instructions.Replace(stringInstruction,
new[]
{
Instruction.Create(OpCodes.Ldsfld, LoggerField),
Instruction.Create(stringInstruction.OpCode, operand),
});
instruction.Operand = ModuleWeaver.GetNormalOperand(methodReference);
return;
}
if (messageVar == null)
{
messageVar = new VariableDefinition(ModuleWeaver.ModuleDefinition.TypeSystem.String);
Method.Body.Variables.Add(messageVar);
}
if (paramsVar == null)
{
paramsVar = new VariableDefinition(ModuleWeaver.ObjectArray);
Method.Body.Variables.Add(paramsVar);
}
instructions.Replace(instruction,
new[]
{
Instruction.Create(OpCodes.Stloc, paramsVar),
Instruction.Create(OpCodes.Stloc, messageVar),
Instruction.Create(OpCodes.Ldsfld, LoggerField),