本文整理汇总了C#中Mono.Cecil.Cil.MethodBody类的典型用法代码示例。如果您正苦于以下问题:C# MethodBody类的具体用法?C# MethodBody怎么用?C# MethodBody使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MethodBody类属于Mono.Cecil.Cil命名空间,在下文中一共展示了MethodBody类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunInternal
protected BlockStatement RunInternal(MethodBody body, BlockStatement block, ILanguage language)
{
try
{
if (body.Instructions.Count != 0 || body.Method.IsJustDecompileGenerated)
{
foreach (IDecompilationStep step in steps)
{
if (language != null && language.IsStopped)
{
break;
}
block = step.Process(Context, block);
}
}
}
finally
{
if (Context.MethodContext.IsMethodBodyChanged)
{
body.Method.RefreshBody();
}
}
return block;
}
示例2: EmitArchsInit
private static void EmitArchsInit(MethodBody body, FieldReference archRef, Action<Instruction> emit)
{
var module = body.Method.Module;
GenericInstanceType dictStrStrRef = (GenericInstanceType)archRef.FieldType;
TypeReference dictOpenRef = dictStrStrRef.ElementType;
GenericInstanceType iEqCompStrRef = new GenericInstanceType(module.Import(typeof(IEqualityComparer<>)));
iEqCompStrRef.GenericArguments.Add(dictOpenRef.GenericParameters[0]);
MethodReference dictStrStrCtor = CecilUtils.ImportInstanceMethodRef(module, dictStrStrRef, ".ctor", null, iEqCompStrRef);
MethodReference dictAddRef = CecilUtils.ImportInstanceMethodRef(module, dictStrStrRef, "Add", null, dictOpenRef.GenericParameters[0], dictOpenRef.GenericParameters[1]);
// Variables
body.Variables.Add(new VariableDefinition(dictStrStrRef));
int varIdx = body.Variables.Count - 1;
Instruction varSt = CecilUtils.ShortestStloc(varIdx);
Instruction varLd = CecilUtils.ShortestLdloc(varIdx);
emit(Instruction.Create(OpCodes.Ldnull));
emit(Instruction.Create(OpCodes.Newobj, dictStrStrCtor));
emit(varSt.Clone());
emit(varLd.Clone());
emit(Instruction.Create(OpCodes.Stsfld, archRef));
Action<string, string> emitAddPair = (k, v) =>
{
emit(varLd.Clone());
emit(Instruction.Create(OpCodes.Ldstr, k));
emit(Instruction.Create(OpCodes.Ldstr, v));
emit(Instruction.Create(OpCodes.Callvirt, dictAddRef));
};
emitAddPair("x86", "Win32");
emitAddPair("AMD64", "x64");
emitAddPair("IA64", "Itanium");
emitAddPair("ARM", "WinCE");
}
示例3: ProxyMethod
protected override void ProxyMethod(MethodBody body, MethodReference proceedTargetMethod)
{
// Create base call
if (!Method.IsAbstract)
{
callBaseMethod = new MethodDefinition(Name + "$Base", MethodAttributes.Private, Method.ReturnType);
Method.CopyParameters(callBaseMethod);
Method.CopyGenericParameters(callBaseMethod);
callBaseMethod.Body.Emit(il =>
{
il.Emit(OpCodes.Ldarg_0);
for (var i = 0; i < Method.Parameters.Count; i++)
{
il.Emit(OpCodes.Ldarg, (short)i + 1);
}
var methodReference = GetProceedMethodTarget();
if (Method.GenericParameters.Count > 0)
methodReference = methodReference.MakeGenericMethod(callBaseMethod.GenericParameters.ToArray());
il.Emit(OpCodes.Call, methodReference);
il.Emit(OpCodes.Ret);
});
ClassWeaver.ProxyType.Methods.Add(callBaseMethod);
}
base.ProxyMethod(body, proceedTargetMethod);
}
示例4: VisitMethodBody
public override void VisitMethodBody (MethodBody body)
{
MethodDefinition meth = body.Method;
MethodBody methBody = body;
BinaryReader br = m_reflectReader.Module.ImageReader.MetadataReader.GetDataReader (meth.RVA);
// lets read the method
int flags = br.ReadByte ();
switch (flags & 0x3) {
case (int) MethodHeader.TinyFormat :
methBody.CodeSize = flags >> 2;
methBody.MaxStack = 8;
ReadCilBody (methBody, br);
break;
case (int) MethodHeader.FatFormat :
br.BaseStream.Position--;
int fatflags = br.ReadUInt16 ();
//int headersize = (fatflags >> 12) & 0xf;
methBody.MaxStack = br.ReadUInt16 ();
methBody.CodeSize = br.ReadInt32 ();
methBody.LocalVarToken = br.ReadInt32 ();
body.InitLocals = (fatflags & (int) MethodHeader.InitLocals) != 0;
if (methBody.LocalVarToken != 0)
VisitVariableDefinitionCollection (methBody.Variables);
ReadCilBody (methBody, br);
if ((fatflags & (int) MethodHeader.MoreSects) != 0)
ReadSection (methBody, br);
break;
}
}
示例5: Replace
public void Replace(MethodBody currentBody, ICollection<MethodReference> modifiedItems)
{
var invalidCalls = _callFilter.GetInvalidCalls(currentBody, modifiedItems);
if (invalidCalls.Count == 0)
return;
var currentInstructions = currentBody.Instructions.Cast<Instruction>().ToArray();
var stackCtor = _targetModule.ImportConstructor<Stack<object>>(new Type[0]);
var IL = currentBody.CilWorker;
var targetMethod = currentBody.Method;
var currentArgument = targetMethod.AddLocal<object>();
var currentArguments = targetMethod.AddLocal<Stack<object>>();
currentBody.Instructions.Clear();
// Create the stack that will hold the method arguments
IL.Emit(OpCodes.Newobj, stackCtor);
IL.Emit(OpCodes.Stloc, currentArguments);
foreach (var currentInstruction in currentInstructions)
{
var currentMethod = currentInstruction.Operand as MethodReference;
// Ignore any instructions that weren't affected by the
// interface extraction
if (currentMethod != null && invalidCalls.ContainsKey(currentMethod))
{
var context = new MethodContext(IL, currentArguments, currentMethod, currentArgument);
_replaceMethodCall.Replace(context, _targetModule);
}
IL.Append(currentInstruction);
}
}
示例6: TerminateMethodBody
public override void TerminateMethodBody(MethodBody body)
{
long pos = m_binaryWriter.BaseStream.Position;
if (body.Variables.Count > 0 || body.ExceptionHandlers.Count > 0
|| m_codeWriter.BaseStream.Length >= 64 || body.MaxStack > 8) {
MethodHeader header = MethodHeader.FatFormat;
if (body.InitLocals)
header |= MethodHeader.InitLocals;
if (body.ExceptionHandlers.Count > 0)
header |= MethodHeader.MoreSects;
m_binaryWriter.Write ((byte) header);
m_binaryWriter.Write ((byte) 0x30); // (header size / 4) << 4
m_binaryWriter.Write ((short) body.MaxStack);
m_binaryWriter.Write ((int) m_codeWriter.BaseStream.Length);
m_binaryWriter.Write (((int) TokenType.Signature | body.LocalVarToken));
WriteExceptionHandlerCollection (body.ExceptionHandlers);
} else
m_binaryWriter.Write ((byte) ((byte) MethodHeader.TinyFormat |
m_codeWriter.BaseStream.Length << 2));
m_binaryWriter.Write (m_codeWriter);
m_binaryWriter.QuadAlign ();
m_reflectWriter.MetadataWriter.AddData (
(int) (m_binaryWriter.BaseStream.Position - pos));
}
示例7: CreateScopes
void CreateScopes (MethodBody body, ScopeCollection scopes, SymbolToken localVarToken)
{
foreach (Scope s in scopes) {
int startOffset = s.Start.Offset;
int endOffset = s.End == body.Instructions.Outside ?
body.Instructions[body.Instructions.Count - 1].Offset + 1 :
s.End.Offset;
m_writer.OpenScope (startOffset);
m_writer.UsingNamespace (body.Method.DeclaringType.Namespace);
m_writer.OpenNamespace (body.Method.DeclaringType.Namespace);
int start = body.Instructions.IndexOf (s.Start);
int end = s.End == body.Instructions.Outside ?
body.Instructions.Count - 1 :
body.Instructions.IndexOf (s.End);
ArrayList instructions = CollectSequencePoints (body, start, end);
DefineSequencePoints (instructions);
CreateLocalVariable (s, startOffset, endOffset, localVarToken);
CreateScopes (body, s.Scopes, localVarToken);
m_writer.CloseNamespace ();
m_writer.CloseScope (endOffset);
}
}
示例8: DecompilationContext
internal DecompilationContext (MethodBody body, ControlFlowGraph cfg)
{
this.body = body;
this.method = body.Method;
this.variables = CloneCollection (body.Variables);
this.cfg = cfg;
}
示例9: CreateExplicitStub
/// <summary>
/// Create a new method in the declaring type of the given implicit implementation with the given name.
/// This method will call the implicit implementation.
/// </summary>
internal static MethodDefinition CreateExplicitStub(MethodDefinition implicitImpl, string name, MethodDefinition iMethod, bool avoidGenericParam)
{
// Create method
var newMethod = new MethodDefinition(name, implicitImpl.Attributes, implicitImpl.ReturnType);
newMethod.IsVirtual = false;
newMethod.IsAbstract = false;
newMethod.IsFinal = true;
// Clone generic parameters
foreach (var gp in implicitImpl.GenericParameters)
{
newMethod.GenericParameters.Add(new GenericParameter(gp.Name, newMethod));
}
// Update according to new context
var cloner = new TypeCloner(avoidGenericParam, implicitImpl.Module.TypeSystem);
newMethod.ReturnType = cloner.Get(implicitImpl.ReturnType, newMethod);
// Clone parameters
foreach (var p in iMethod.Parameters)
{
newMethod.Parameters.Add(new ParameterDefinition(p.Name, p.Attributes, cloner.Get(p.ParameterType, newMethod)));
}
// Add the method
var targetType = implicitImpl.DeclaringType;
targetType.Methods.Add(newMethod);
// Add override
newMethod.Overrides.Add(iMethod);
// Create method body
var body = new MethodBody(newMethod);
newMethod.Body = body;
var worker = body.GetILProcessor();
// Push this
worker.Emit(OpCodes.Ldarg, body.ThisParameter);
for (var i = 0; i < implicitImpl.Parameters.Count; i++)
{
var p = iMethod.Parameters[i];
var newMethodParam = newMethod.Parameters[i];
worker.Emit(OpCodes.Ldarg, newMethodParam);
if (/*avoidGenericParam &&*/ p.ParameterType.ContainsGenericParameter)
{
worker.Emit(OpCodes.Box, implicitImpl.Parameters[i].ParameterType);
}
}
worker.Emit(implicitImpl.IsVirtual ? OpCodes.Callvirt : OpCodes.Call, implicitImpl);
worker.Emit(OpCodes.Ret);
// Mark method reachable
if (implicitImpl.IsReachable)
{
newMethod.SetReachable(null);
}
return newMethod;
}
示例10: SwapMethods
private static void SwapMethods(MethodBody body, IEnumerable<Instruction> oldInstructions, IDictionary<MethodReference, MethodReference> methodMap,
TypeDefinition targetDependency)
{
var IL = body.CilWorker;
foreach (var instruction in oldInstructions)
{
var opCode = instruction.OpCode;
if (opCode != OpCodes.Call && opCode != OpCodes.Callvirt)
{
IL.Append(instruction);
continue;
}
var currentMethod = instruction.Operand as MethodReference;
if (currentMethod == null || !methodMap.ContainsKey(currentMethod))
{
IL.Append(instruction);
continue;
}
if (currentMethod.DeclaringType != targetDependency)
{
IL.Append(instruction);
continue;
}
var interfaceMethod = methodMap[currentMethod];
instruction.Operand = interfaceMethod;
instruction.OpCode = OpCodes.Callvirt;
IL.Append(instruction);
}
}
示例11: Write
public void Write(MethodBody body, /*Telerik Authorship*/ MetadataToken methodToken, /*Telerik Authorship*/ MetadataToken localVarToken)
{
var method = new SourceMethod (body.Method);
var instructions = GetInstructions (body);
int count = instructions.Count;
if (count == 0)
return;
var offsets = new int [count];
var start_rows = new int [count];
var start_cols = new int [count];
SourceFile file;
Populate (instructions, offsets, start_rows, start_cols, out file);
var builder = writer.OpenMethod (file.CompilationUnit, 0, method);
for (int i = 0; i < count; i++)
builder.MarkSequencePoint (
offsets [i],
file.CompilationUnit.SourceFile,
start_rows [i],
start_cols [i],
false);
if (body.HasVariables)
AddVariables (body.Variables);
writer.CloseMethod ();
}
示例12: VisitMethodBody
public void VisitMethodBody(MethodBody body)
{
this.MethodBody = body;
//VisitVariableDefinitionCollection(body.Variables);
//VisitInstructionCollection(body.Instructions);
//VisitExceptionHandlerCollection(body.ExceptionHandlers);
}
示例13: Write
public void Write(MethodBody body, byte [][] variables)
{
CreateDocuments (body);
m_writer.OpenMethod (new SymbolToken ((int) body.Method.MetadataToken.ToUInt ()));
CreateScopes (body, body.Scopes, variables);
m_writer.CloseMethod ();
}
示例14: ContainsCallTo
public static bool ContainsCallTo(MethodBody m,
string methodFullName)
{
return m.Instructions.Any(i =>
(i.OpCode == OpCodes.Call || i.OpCode == OpCodes.Callvirt) &&
((MethodReference)i.Operand).FullName == methodFullName);
}
示例15: Write
public void Write(MethodBody body)
{
var method_token = body.Method.MetadataToken;
var sym_token = new SymbolToken (method_token.ToInt32 ());
var instructions = CollectInstructions (body);
if (instructions.Count == 0 && !body.HasVariables)
return;
writer.OpenMethod (sym_token);
DefineSequencePoints (instructions);
if (body.Scope != null)
WriteScope (body, body.Scope);
else
if (body.HasVariables)
{
var start_offset = 0;
var end_offset = body.CodeSize;
writer.OpenScope (start_offset);
DefineVariables (body, body.Variables, start_offset, end_offset);
writer.CloseScope (end_offset);
}
if (body.IteratorType != null)
DefineIteratorType (sym_token, body.IteratorType.Name);
if (body.iterator_scopes != null)
DefineIteratorScopes (sym_token, body.IteratorScopes, body.CodeSize);
writer.CloseMethod ();
}