本文整理汇总了C#中System.Reflection.PortableExecutable.PEReader.GetMethodBody方法的典型用法代码示例。如果您正苦于以下问题:C# PEReader.GetMethodBody方法的具体用法?C# PEReader.GetMethodBody怎么用?C# PEReader.GetMethodBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.PortableExecutable.PEReader
的用法示例。
在下文中一共展示了PEReader.GetMethodBody方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IL_LazyLoad
public void IL_LazyLoad()
{
var peStream = new MemoryStream(TestResources.Misc.Members);
using (var reader = new PEReader(peStream, PEStreamOptions.LeaveOpen))
{
var md = reader.GetMetadataReader();
var il = reader.GetMethodBody(md.GetMethodDefinition(MetadataTokens.MethodDefinitionHandle(1)).RelativeVirtualAddress);
Assert.Equal(new byte[] { 0, 42 }, il.GetILBytes());
Assert.Equal(8, il.MaxStack);
}
}
示例2: GetILBytes
/// <remarks>
/// Based on https://gist.github.com/nguerrera/72444715c7ea0b40addb
/// </remarks>
public static byte[] GetILBytes(this MethodInfo methodInfo)
{
#if NET45
return methodInfo.GetMethodBody().GetILAsByteArray();
#else
var metadataToken = methodInfo.GetMetadataToken();
using (var stream = File.OpenRead(methodInfo.DeclaringType.GetTypeInfo().Assembly.Location))
using (var peReader = new PEReader(stream))
{
var metadataReader = peReader.GetMetadataReader();
var methodHandle = MetadataTokens.MethodDefinitionHandle(metadataToken);
var methodDefinition = metadataReader.GetMethodDefinition(methodHandle);
var methodBody = peReader.GetMethodBody(methodDefinition.RelativeVirtualAddress);
return methodBody.GetILBytes();
}
#endif
}
示例3: Metadata_EagerLoad
public void Metadata_EagerLoad()
{
var peStream = new MemoryStream(TestResources.Misc.Members);
using (var reader = new PEReader(peStream, PEStreamOptions.LeaveOpen | PEStreamOptions.PrefetchMetadata))
{
var md = reader.GetMetadataReader();
var method = md.GetMethodDefinition(MetadataTokens.MethodDefinitionHandle(1));
Assert.Equal("MC1", md.GetString(method.Name));
Assert.Throws<InvalidOperationException>(() => reader.GetEntireImage());
Assert.Throws<InvalidOperationException>(() => reader.GetMethodBody(method.RelativeVirtualAddress));
}
}