本文整理汇总了C#中System.Reflection.MethodInfo.GetMethodBody方法的典型用法代码示例。如果您正苦于以下问题:C# MethodInfo.GetMethodBody方法的具体用法?C# MethodInfo.GetMethodBody怎么用?C# MethodInfo.GetMethodBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.MethodInfo
的用法示例。
在下文中一共展示了MethodInfo.GetMethodBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MethodBodyReader
/// <summary>
/// MethodBodyReader constructor
/// </summary>
/// <param name="mi">
/// The System.Reflection defined MethodInfo
/// </param>
public MethodBodyReader(MethodInfo mi)
{
if (mi.GetMethodBody() != null)
{
il = mi.GetMethodBody().GetILAsByteArray();
ConstructInstructions(mi.Module);
}
}
示例2: EventOperation
public EventOperation(Delegate assignation)
{
_delegateMethod = assignation.Method;
MethodBody body = _delegateMethod.GetMethodBody();
_stream = new MemoryStream(body.GetILAsByteArray());
_module = _delegateMethod.Module;
}
示例3: IsSimpleSetter
internal static bool IsSimpleSetter(MethodInfo setter, out FieldInfo field)
{
MethodBody body = setter.GetMethodBody();
bool isSimple = false;
// out param
field = null;
if (body.ExceptionHandlingClauses.Count == 0 && body.LocalVariables.Count == 0)
{
byte[] il = body.GetILAsByteArray();
if (il.Length == 8)
{
if (il[0] == OpCodes.Ldarg_0.Value
&& il[1] == OpCodes.Ldarg_1.Value
&& il[2] == OpCodes.Stfld.Value
&& il[7] == OpCodes.Ret.Value)
{
int fieldToken = BitConverter.ToInt32(il, 3);
FieldInfo info = setter.DeclaringType.Module.ResolveField(fieldToken);
if (info != null
&& info.DeclaringType.IsAssignableFrom(setter.DeclaringType))
{
isSimple = true;
field = info;
}
}
}
}
return isSimple;
}
示例4: ValidateMethod
/// <summary>
/// Validates an actual method (the cloned version) against an expected method (its source).
/// </summary>
/// <param name="actualMethod">Method to validate.</param>
/// <param name="expectedMethod">Method with expected info.</param>
public static void ValidateMethod(MethodInfo actualMethod, MethodInfo expectedMethod)
{
Assert.That(actualMethod == null, Is.EqualTo(expectedMethod == null));
if (actualMethod == null) { return; }
Assert.That(actualMethod.Name, Is.EqualTo(expectedMethod.Name));
// TODO more things?
ValidateMethodBody(actualMethod.GetMethodBody(), expectedMethod.GetMethodBody());
}
示例5: ProcessMethod
static void ProcessMethod(MethodInfo m)
{
Console.WriteLine("Processing Method: {0}::{1}", m.DeclaringType.FullName, m.Name);
var body = m.GetMethodBody();
Console.WriteLine("Return type: {0}", m.ReturnType);
Console.WriteLine("Local variable layout:");
foreach (var lv in body.LocalVariables)
{
Console.WriteLine("\t{0}: type {1} pinned {2}", lv.LocalIndex, lv.LocalType, lv.IsPinned);
}
Console.WriteLine("Code bytes:");
var code = body.GetILAsByteArray();
var recog = new CilRecognizer(code, new TokenResolver(m));
var stack = new Type[] { };
Func<Type[], string> StringOfStack =
s => string.Join( " ", s.Select(a=>a.ToString()).ToArray() );
foreach (var oc in recog.GetInstructions())
{
Console.WriteLine("{0}", oc.ToString());
// try to generate some code
var impls = CodeGenerator.FindMatches(oc, stack).ToArray();
if (impls.Length == 0)
{
Console.WriteLine("--- ERROR: No code generation option ----");
Console.WriteLine("Stack: " + StringOfStack(stack));
Console.WriteLine();
}
else
{
Console.WriteLine("Emitting from: {0}", impls[0].Definition);
foreach( var s in impls[0].SpecializeCodeForContext( oc, stack ) )
Console.WriteLine(s);
stack = impls[0].AdjustStack(oc, stack);
}
}
Console.WriteLine();
}
示例6: MethodBodyDecompiler
public MethodBodyDecompiler(MethodInfo method)
{
locals = new Expression[0];
this.method = method;
var parameters = method.GetParameters();
if (method.IsStatic)
args = parameters
.Select(p => Expression.Parameter(p.ParameterType, p.Name))
.ToList();
else
args = new[] { Expression.Parameter(method.DeclaringType, "this") }
.Union(parameters.Select(p => Expression.Parameter(p.ParameterType, p.Name)))
.ToList();
var body = method.GetMethodBody();
locals = new Expression[body.LocalVariables.Count];
}
示例7: Walk
public void Walk(MethodInfo method, ControlFlowGraph graph)
{
this.recorder = new CallParameterTypesRecorder();
this.recorder.Initialize(method);
var variables = method.GetMethodBody().LocalVariables.ToDictionary(x => x.LocalIndex, x => PotentialType.FromType(x.LocalType));
var parameters = method.GetParameters().ToDictionary(x => x.Position, x => PotentialType.FromType(x.ParameterType));
var initialState = new TypeAnalysisState(variables, parameters);
var walker = new ControlFlowGraphWalker<TypeAnalysisState>
{
InitialState = initialState,
VisitingBlock = (state, block) => this.recorder.Visit(state, block.Instructions)
};
walker.WalkCore(method, graph);
}
示例8: MethodBodyDecompiler
public MethodBodyDecompiler(MethodInfo method)
{
this.method = method;
var parameters = method.GetParameters();
if (method.IsStatic)
args = parameters
.Select(p => (Address) Expression.Parameter(p.ParameterType, p.Name))
.ToList();
else
args = new[] {(Address) Expression.Parameter(method.DeclaringType, "this")}
.Union(parameters.Select(p => (Address) Expression.Parameter(p.ParameterType, p.Name)))
.ToList();
var body = method.GetMethodBody();
var addresses = new VariableInfo[body.LocalVariables.Count];
for (int i = 0; i < addresses.Length; i++)
{
addresses[i] = new VariableInfo(body.LocalVariables[i].LocalType);
}
locals = addresses.ToArray();
}
示例9: AreMethodsEqualForDeclaringType
/// <summary>
/// Returns a value indicating whether two instances of <see cref="MethodInfo"/> are equal
/// for a declaring type.
/// </summary>
/// <param name="first">
/// The first <see cref="MethodInfo"/>.
/// </param>
/// <param name="second">
/// The second <see cref="MethodInfo"/>.
/// </param>
/// <returns>
/// True if the two instances of <see cref="MethodInfo"/> are equal; otherwise, false.
/// </returns>
private bool AreMethodsEqualForDeclaringType(MethodInfo first, MethodInfo second)
{
byte[] firstBytes = { };
byte[] secondBytes = { };
if (first != null && first.ReflectedType != null && first.DeclaringType != null)
{
first = first.ReflectedType == first.DeclaringType ? first
: first.DeclaringType.GetMethod(
first.Name,
first.GetParameters().Select(p => p.ParameterType).ToArray());
MethodBody body = first.GetMethodBody();
if (body != null)
{
firstBytes = body.GetILAsByteArray();
}
}
if (second != null && second.ReflectedType != null && second.DeclaringType != null)
{
second = second.ReflectedType == second.DeclaringType
? second
: second.DeclaringType.GetMethod(
second.Name,
second.GetParameters().Select(p => p.ParameterType).ToArray());
MethodBody body = second.GetMethodBody();
if (body != null)
{
secondBytes = body.GetILAsByteArray();
}
}
return firstBytes.SequenceEqual(secondBytes);
}
示例10: DisassembleMethod
static object DisassembleMethod(MethodInfo meth)
{
Console.WriteLine(meth);
var locals = meth.GetMethodBody().LocalVariables;
if (locals.Count > 0)
{
Console.WriteLine(".locals init (");
foreach (var l in locals)
{
Console.WriteLine(" {0}", l);
}
Console.WriteLine(")");
}
foreach (var inst in Reflection.Disassembler.GetInstructions(meth))
{
Console.WriteLine(inst);
}
return Unspecified;
}
示例11: CompareMethodsDifferent
private static bool CompareMethodsDifferent(MethodInfo m1info, MethodInfo m2info)
{
var m1 = m1info.GetMethodBody();
var m2 = m2info.GetMethodBody();
if (m1 != null)
{
Debug.Assert(m2 != null);
var m1body = m1.GetILAsByteArray();
var m2body = m2.GetILAsByteArray();
if (m1body.Length != m2body.Length)
{
return true;
}
var r1set = m1body.ZIP(m2body, (a, b) => a == b);
var r1setd = r1set.Distinct();
var r1 = r1setd.Count() > 1;
return r1;
}
return false;
}
示例12: CalculateStackProperties
public override void CalculateStackProperties(MethodInfo containingMethod)
{
var methodBody = containingMethod.GetMethodBody();
int stackValue = 0;
foreach (var instruction in Instructions)
{
stackValue -= instruction.PopedValuesCount(containingMethod);
this.GoesBelowInitialStack = this.GoesBelowInitialStack || stackValue < 0;
this.SetsLocalVariable = this.SetsLocalVariable || instruction.OpCode.IsStoreVariable();
stackValue += instruction.PushedValuesCount(containingMethod, methodBody);
}
this.StackDiff = stackValue;
this.InStackHeight = this.TransitedFrom.Select(x => (int?)x.OutStackHeight).Distinct().SingleOrDefault() ?? 0;
this.OutStackHeight = this.InStackHeight + this.StackDiff;
}
示例13: EmitFunction
private static Function EmitFunction(Context context, Module module, MethodInfo method)
{
var funcType = new FunctionType(ConvertType(context, method.ReturnType), AnalyzeArguments(context, method.GetParameters()));
var intrinsic = method.GetCustomAttribute<Gpu.BuiltinAttribute>();
if (intrinsic != null)
{
var name = intrinsic.Intrinsic;
var preExisting = module.GetFunction(name);
if (preExisting != null)
return preExisting;
return module.CreateFunction(name, funcType);
}
var function = module.CreateFunction(method.Name, funcType);
var block = new Block("entry", context, function);
var writer = new InstructionBuilder(context, block);
var opcodes = method.Decompile().ToList();
FindBranchTargets(opcodes, context, function);
var body = method.GetMethodBody();
var efo = new EmitFuncObj(context, module, function, writer, null, new Stack<Value>(),
body == null ? null : new Value[body.LocalVariables.Count], new Value[method.GetParameters().Length]);
foreach (var opcode in opcodes)
{
if (EmitFunctions.ContainsKey(opcode.Opcode) == false)
throw new Exception("Unsupported CIL instruction " + opcode.Opcode);
var func = EmitFunctions[opcode.Opcode];
efo.Argument = opcode.Parameter;
func(efo);
}
return function;
}
示例14: GetHelperCallFirstArg
private static int GetHelperCallFirstArg(MethodInfo method)
{
byte[] iLAsByteArray = method.GetMethodBody().GetILAsByteArray();
int num = -1;
for (int i = 0; i < iLAsByteArray.Length; i++)
{
int num3;
switch (iLAsByteArray[i])
{
case 140:
case 0x8d:
{
i += 4;
continue;
}
case 0xa2:
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 20:
case 0x25:
case 0x67:
case 0x68:
case 0x69:
case 0x6a:
case 0x6d:
case 110:
{
continue;
}
case 0xfe:
i++;
if ((i < iLAsByteArray.Length) && (iLAsByteArray[i] < 6))
{
continue;
}
goto Label_0206;
case 14:
case 0x10:
{
i++;
continue;
}
case 0x15:
case 0x16:
case 0x17:
case 0x18:
case 0x19:
case 0x1a:
case 0x1b:
case 0x1c:
case 0x1d:
case 30:
{
if ((i > 0) && (iLAsByteArray[i - 1] == 2))
{
num = iLAsByteArray[i] - 0x16;
}
continue;
}
case 0x1f:
{
if ((i > 0) && (iLAsByteArray[i - 1] == 2))
{
num = iLAsByteArray[i + 1];
}
i++;
continue;
}
case 0x20:
{
i += 4;
continue;
}
case 40:
i += 4;
if (num < 0)
{
goto Label_01DC;
}
num3 = i + 1;
goto Label_01D6;
case 0x2c:
case 0x2d:
{
num = -1;
i++;
//.........这里部分代码省略.........
示例15: GetHelperCallFirstArg
static private int GetHelperCallFirstArg(MethodInfo method)
{
// Currently searches for the following pattern
//
// ... // CAN ONLY BE THE INSTRUCTIONS BELOW
// LDARG0
// LDC.I4 XXX
// ... // CAN ONLY BE THE INSTRUCTIONS BELOW CAN'T BE A BRANCH OR A CALL
// CALL
// NOP // 0 or more times
// RET
//
// If we find this pattern we return the XXX. Otherwise we return -1.
byte[] instrs = method.GetMethodBody().GetILAsByteArray();
int retVal = -1;
for (int idx = 0; idx < instrs.Length; )
{
switch (instrs[idx])
{
case 0: // NOP
case 1: // BREAK
case 2: // LDARG_0
case 3: // LDARG_1
case 4: // LDARG_2
case 5: // LDARG_3
case 6: // LDLOC_0
case 7: // LDLOC_1
case 8: // LDLOC_2
case 9: // LDLOC_3
case 10: // STLOC_0
case 11: // STLOC_1
case 12: // STLOC_2
case 13: // STLOC_3
break;
case 14: // LDARG_S
case 16: // STARG_S
idx++;
break;
case 20: // LDNULL
break;
case 21: // LDC_I4_M1
case 22: // LDC_I4_0
case 23: // LDC_I4_1
case 24: // LDC_I4_2
case 25: // LDC_I4_3
case 26: // LDC_I4_4
case 27: // LDC_I4_5
case 28: // LDC_I4_6
case 29: // LDC_I4_7
case 30: // LDC_I4_8
if (idx > 0 && instrs[idx - 1] == 2) // preceeded by LDARG0
retVal = instrs[idx] - 22;
break;
case 31: // LDC_I4_S
if (idx > 0 && instrs[idx - 1] == 2) // preceeded by LDARG0
retVal = instrs[idx + 1];
idx++;
break;
case 32: // LDC_I4
idx += 4;
break;
case 37: // DUP
break;
case 40: // CALL
idx += 4;
if (retVal >= 0)
{
// Is this call just before return?
for (int search = idx + 1; search < instrs.Length; search++)
{
if (instrs[search] == 42) // RET
return retVal;
if (instrs[search] != 0) // NOP
break;
}
}
retVal = -1;
break;
case 44: // BRFALSE_S
case 45: // BRTRUE_S
retVal = -1;
idx++;
break;
case 57: // BRFALSE
case 58: // BRTRUE
retVal = -1;
idx += 4;
break;
case 103: // CONV_I1
case 104: // CONV_I2
case 105: // CONV_I4
case 106: // CONV_I8
case 109: // CONV_U4
case 110: // CONV_U8
break;
case 140: // BOX
case 141: // NEWARR
idx += 4;
break;
//.........这里部分代码省略.........