本文整理汇总了C#中MethodBody.SimplifyMacros方法的典型用法代码示例。如果您正苦于以下问题:C# MethodBody.SimplifyMacros方法的具体用法?C# MethodBody.SimplifyMacros怎么用?C# MethodBody.SimplifyMacros使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MethodBody
的用法示例。
在下文中一共展示了MethodBody.SimplifyMacros方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InnerProcess
public void InnerProcess()
{
body = Method.Body;
body.SimplifyMacros();
InjectStopwatch();
HandleReturns();
body.InitLocals = true;
body.OptimizeMacros();
}
示例2: ContinueProcessing
void ContinueProcessing()
{
body = Method.Body;
body.SimplifyMacros();
var ilProcessor = body.GetILProcessor();
var returnFixer = new ReturnFixer
{
Method = Method
};
returnFixer.MakeLastStatementReturn();
exceptionVariable = new VariableDefinition(ModuleWeaver.ExceptionType);
body.Variables.Add(exceptionVariable);
messageVariable = new VariableDefinition(ModuleWeaver.ModuleDefinition.TypeSystem.String);
body.Variables.Add(messageVariable);
paramsArrayVariable = new VariableDefinition(ModuleWeaver.ObjectArray);
body.Variables.Add(paramsArrayVariable);
var tryCatchLeaveInstructions = Instruction.Create(OpCodes.Leave, returnFixer.NopBeforeReturn);
var methodBodyFirstInstruction = GetMethodBodyFirstInstruction();
var catchInstructions = GetCatchInstructions().ToList();
ilProcessor.InsertBefore(returnFixer.NopBeforeReturn, tryCatchLeaveInstructions);
ilProcessor.InsertBefore(returnFixer.NopBeforeReturn, catchInstructions);
var handler = new ExceptionHandler(ExceptionHandlerType.Catch)
{
CatchType = ModuleWeaver.ExceptionType,
TryStart = methodBodyFirstInstruction,
TryEnd = tryCatchLeaveInstructions.Next,
HandlerStart = catchInstructions.First(),
HandlerEnd = catchInstructions.Last().Next
};
body.ExceptionHandlers.Add(handler);
body.InitLocals = true;
body.OptimizeMacros();
}
示例3: InnerProcess
void InnerProcess()
{
body = Method.Body;
body.SimplifyMacros();
var firstInstruction = FirstInstructionSkipCtor();
var indexOf = body.Instructions.IndexOf(firstInstruction);
var returnInstruction = FixReturns();
stopwatchVar = ModuleWeaver.InjectStopwatch(body, indexOf);
var beforeReturn = Instruction.Create(OpCodes.Nop);
body.InsertBefore(returnInstruction, beforeReturn);
InjectIlForFinally(returnInstruction);
var handler = new ExceptionHandler(ExceptionHandlerType.Finally)
{
TryStart = firstInstruction,
TryEnd = beforeReturn,
HandlerStart = beforeReturn,
HandlerEnd = returnInstruction,
};
body.ExceptionHandlers.Add(handler);
body.InitLocals = true;
body.OptimizeMacros();
}
示例4: ReplaceCalls
private void ReplaceCalls(MethodBody body, Dictionary<TypeDefinition, TypeDefinition> replacements)
{
body.SimplifyMacros();
var calls = body.Instructions.Where(i => i.OpCode == OpCodes.Call);
foreach (var call in calls)
{
var originalMethodReference = (MethodReference)call.Operand;
var originalMethodDefinition = originalMethodReference.Resolve();
var declaringTypeReference = originalMethodReference.DeclaringType;
var declaringTypeDefinition = declaringTypeReference.Resolve();
if (!originalMethodDefinition.IsStatic || !replacements.ContainsKey(declaringTypeDefinition))
continue;
var replacementTypeReference = ModuleDefinition.ImportReference(replacements[declaringTypeDefinition]);
if (declaringTypeReference.IsGenericInstance)
{
var declaringGenericType = (GenericInstanceType)declaringTypeReference;
var genericType = new GenericInstanceType(replacementTypeReference);
foreach (var arg in declaringGenericType.GenericArguments)
{
genericType.GenericArguments.Add(arg);
}
replacementTypeReference = ModuleDefinition.ImportReference(genericType);
}
var replacementMethod = replacementTypeReference.ReferenceMethod(originalMethodDefinition.Name);
if (replacementMethod == null)
{
LogError(String.Format("Missing '{0}.{1}()' in '{2}'", declaringTypeDefinition.FullName, originalMethodDefinition.Name, replacementTypeReference.FullName));
continue;
}
if (!replacementMethod.Resolve().IsStatic)
{
LogError(String.Format("Replacement method '{0}' is not static", replacementMethod.FullName));
continue;
}
if (originalMethodReference.IsGenericInstance)
{
var originalGenericInstanceMethod = (GenericInstanceMethod)originalMethodReference;
var genericInstanceMethod = new GenericInstanceMethod(replacementMethod);
foreach (var arg in originalGenericInstanceMethod.GenericArguments)
{
genericInstanceMethod.GenericArguments.Add(arg);
}
call.Operand = ModuleDefinition.ImportReference(genericInstanceMethod);
}
else
{
call.Operand = replacementMethod;
}
}
body.InitLocals = true;
body.OptimizeMacros();
}
示例5: InnerProcess
void InnerProcess()
{
var asyncAttribute = Method.GetAsyncStateMachineAttribute();
stateMachineType = asyncAttribute.ConstructorArguments
.Select(ctor => (TypeDefinition)ctor.Value)
.Single();
var moveNextMethod = stateMachineType.Methods
.Single(x => x.Name == "MoveNext");
body = moveNextMethod.Body;
body.SimplifyMacros();
returnPoints = GetAsyncReturns(body.Instructions)
.ToList();
// First, fall back to old mechanism
var exceptionHandler = body.ExceptionHandlers.First();
var index = body.Instructions.IndexOf(exceptionHandler.TryStart);
// Check roslyn usage
var firstStateUsage = (from instruction in body.Instructions
let fieldReference = instruction.Operand as FieldReference
where instruction.OpCode == OpCodes.Ldfld &&
fieldReference != null &&
fieldReference.Name.Contains("__state")
select instruction).FirstOrDefault();
if (firstStateUsage == null)
{
// Probably compiled without roslyn, inject at first line
index = 0;
}
else
{
// Initial code looks like this (hence the -1):
//
// <== this is where we want to start the stopwatch
// ldarg.0
// ldfld __state
// stloc.0
// ldloc.0
index = body.Instructions.IndexOf(firstStateUsage) - 1;
}
InjectStopwatch(index, body.Instructions[index]);
HandleReturns();
body.InitLocals = true;
body.OptimizeMacros();
}
示例6: AddUsing
private void AddUsing(MethodBody methodBody, Tuple<Instruction, Instruction> usingBlock)
{
var stloc = usingBlock.Item1.Previous;
while (stloc.OpCode == OpCodes.Nop)
stloc = stloc.Previous;
var variable = (VariableDefinition)stloc.Operand;
if (!variable.VariableType.HasInterface("System.IDisposable") || variable.VariableType.IsArray)
return;
var il = methodBody.GetILProcessor();
var disposeCall = il.Create(OpCodes.Callvirt, ModuleDefinition.ImportReference(typeof(IDisposable).GetMethod("Dispose")));
methodBody.OptimizeMacros();
methodBody.SimplifyMacros();
var tryStart = usingBlock.Item1;
var handlerEnd = usingBlock.Item2.Next;
Instruction leave;
if (usingBlock.Item1 == usingBlock.Item2)
{
// Empty using
leave = il.Create(OpCodes.Leave, usingBlock.Item1);
il.InsertBefore(usingBlock.Item2, leave);
tryStart = leave;
handlerEnd = leave.Next;
}
else if (usingBlock.Item2.OpCode == OpCodes.Br)
{
leave = il.Create(OpCodes.Leave, (Instruction)usingBlock.Item2.Operand);
il.Replace(usingBlock.Item2, leave);
}
else
{
leave = il.Create(OpCodes.Leave, usingBlock.Item2.Next);
il.InsertAfter(usingBlock.Item2, leave);
}
var firstPartOfFinally = il.Create(OpCodes.Ldloc, variable);
var endFinally = il.Create(OpCodes.Endfinally);
if (variable.VariableType.IsGenericParameter)
{
il.InsertAfter(leave,
firstPartOfFinally,
il.Create(OpCodes.Box, variable.VariableType),
il.Create(OpCodes.Brfalse, endFinally),
il.Create(OpCodes.Ldloca, variable),
il.Create(OpCodes.Constrained, variable.VariableType),
disposeCall,
endFinally
);
}
else
{
il.InsertAfter(leave,
firstPartOfFinally,
il.Create(OpCodes.Brfalse, endFinally),
il.Create(OpCodes.Ldloc, variable),
disposeCall,
endFinally
);
}
var handler = new ExceptionHandler(ExceptionHandlerType.Finally)
{
TryStart = tryStart,
TryEnd = firstPartOfFinally,
HandlerStart = firstPartOfFinally,
HandlerEnd = handlerEnd,
};
methodBody.ExceptionHandlers.Add(handler);
}