本文整理汇总了C#中System.Reflection.PortableExecutable.PEReader.GetMetadataReader方法的典型用法代码示例。如果您正苦于以下问题:C# PEReader.GetMetadataReader方法的具体用法?C# PEReader.GetMetadataReader怎么用?C# PEReader.GetMetadataReader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.PortableExecutable.PEReader
的用法示例。
在下文中一共展示了PEReader.GetMetadataReader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
if (args.Length == 0 || new[] {"/?", "-?", "-h", "--help"}.Any(x => string.Equals(args[0], x, StringComparison.OrdinalIgnoreCase)))
{
PrintUsage();
return;
}
foreach (var fileName in args)
{
Console.WriteLine(fileName);
Console.WriteLine(new string('*', 80));
try
{
using (var stream = File.OpenRead(fileName))
using (var peFile = new PEReader(stream))
{
var metadataReader = peFile.GetMetadataReader();
var visualizer = new MetadataVisualizer(metadataReader, Console.Out);
visualizer.Visualize();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
示例2: PE
public PE(string fileName)
{
FileName = Path.GetFullPath(fileName);
Uri = new Uri(FileName);
IsPEFile = false;
try
{
_fs = File.OpenRead(FileName);
byte byteRead = (byte)_fs.ReadByte();
if (byteRead != 'M') { return; }
byteRead = (byte)_fs.ReadByte();
if (byteRead != 'Z') { return; }
_fs.Seek(0, SeekOrigin.Begin);
_peReader = new PEReader(_fs);
PEHeaders = _peReader.PEHeaders;
IsPEFile = true;
}
catch (IOException e) { LoadException = e; }
catch (BadImageFormatException e) { LoadException = e; }
catch (UnauthorizedAccessException e) { LoadException = e; }
if (IsPEFile)
{
m_pImage = new SafePointer(_peReader.GetEntireImage().GetContent().ToBuilder().ToArray());
if (IsManaged)
{
_metadataReader = _peReader.GetMetadataReader();
}
}
}
示例3: Validate
/// <summary>
/// Returns true if the PE file meets all of the pre-conditions to be Open Source Signed.
/// Returns false and logs msbuild errors otherwise.
/// </summary>
private static bool Validate(PEReader peReader)
{
if (!peReader.HasMetadata)
{
Console.Error.WriteLine("PE file is not a managed module.");
return false;
}
var mdReader = peReader.GetMetadataReader();
if (!mdReader.IsAssembly)
{
Console.Error.WriteLine("PE file is not an assembly.");
return false;
}
CorHeader header = peReader.PEHeaders.CorHeader;
if ((header.Flags & CorFlags.StrongNameSigned) == CorFlags.StrongNameSigned)
{
Console.Error.WriteLine("PE file is already strong-name signed.");
return false;
}
if ((header.StrongNameSignatureDirectory.Size <= 0) || mdReader.GetAssemblyDefinition().PublicKey.IsNil)
{
Console.Error.WriteLine("PE file is not a delay-signed assembly.");
return false;
}
return true;
}
示例4: InspectableAssembly
public InspectableAssembly(string filename, ParseContext context)
{
Stream = File.OpenRead(filename);
Pe = new PEReader(Stream);
Reader = Pe.GetMetadataReader();
Context = context;
}
示例5: TryGetAssemblyIdentity
public static AssemblyIdentity TryGetAssemblyIdentity(string filePath)
{
try
{
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete))
using (var peReader = new PEReader(stream))
{
var metadataReader = peReader.GetMetadataReader();
AssemblyDefinition assemblyDefinition = metadataReader.GetAssemblyDefinition();
string name = metadataReader.GetString(assemblyDefinition.Name);
Version version = assemblyDefinition.Version;
StringHandle cultureHandle = assemblyDefinition.Culture;
string cultureName = (!cultureHandle.IsNil) ? metadataReader.GetString(cultureHandle) : null;
AssemblyFlags flags = assemblyDefinition.Flags;
bool hasPublicKey = (flags & AssemblyFlags.PublicKey) != 0;
BlobHandle publicKeyHandle = assemblyDefinition.PublicKey;
ImmutableArray<byte> publicKeyOrToken = !publicKeyHandle.IsNil
? metadataReader.GetBlobBytes(publicKeyHandle).AsImmutableOrNull()
: default(ImmutableArray<byte>);
return new AssemblyIdentity(name, version, cultureName, publicKeyOrToken, hasPublicKey);
}
}
catch { }
return null;
}
示例6: OpenNativeImage
public void OpenNativeImage()
{
using (var reader = new PEReader(File.OpenRead(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "kernel32.dll"))))
{
Assert.False(reader.HasMetadata);
Assert.True(reader.PEHeaders.IsDll);
Assert.False(reader.PEHeaders.IsExe);
Assert.Throws<InvalidOperationException>(() => reader.GetMetadataReader());
}
}
示例7: Execute
public override bool Execute()
{
if (Assemblies == null || Assemblies.Length == 0)
return true;
List<ITaskItem> references = new List<ITaskItem>();
List<ITaskItem> nativeLibs = new List<ITaskItem>();
foreach (var assemblyItem in Assemblies)
{
try
{
if (!File.Exists(assemblyItem.ItemSpec))
{
Log.LogError($"File {assemblyItem.ItemSpec} does not exist, ensure you have built libraries before building the package.");
continue;
}
using (PEReader peReader = new PEReader(new FileStream(assemblyItem.ItemSpec, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.Read)))
{
MetadataReader reader = peReader.GetMetadataReader();
foreach (var handle in reader.AssemblyReferences)
{
AssemblyReference reference = reader.GetAssemblyReference(handle);
TaskItem referenceItem = new TaskItem(reader.GetString(reference.Name));
assemblyItem.CopyMetadataTo(referenceItem);
referenceItem.SetMetadata("Version", reference.Version.ToString());
referenceItem.SetMetadata("AssemblyVersion", reference.Version.ToString());
references.Add(referenceItem);
}
for (int i = 1, count = reader.GetTableRowCount(TableIndex.ModuleRef); i <= count; i++)
{
var moduleRef = reader.GetModuleReference(MetadataTokens.ModuleReferenceHandle(i));
var moduleName = reader.GetString(moduleRef.Name);
TaskItem nativeLib = new TaskItem(moduleName);
assemblyItem.CopyMetadataTo(nativeLib);
nativeLibs.Add(nativeLib);
}
}
}
catch (InvalidOperationException)
{
// Ignore invalid assemblies
}
}
ReferencedAssemblies = references.ToArray();
ReferencedNativeLibraries = nativeLibs.ToArray();
return true;
}
示例8: CreateReader
public static ISymUnmanagedReader CreateReader(Stream pdbStream, Stream peStreamOpt = null)
{
if (peStreamOpt != null)
{
var peReader = new PEReader(peStreamOpt);
return CreateReader(pdbStream, peReader.GetMetadataReader(), peReader);
}
else
{
return CreateReader(pdbStream, null, null);
}
}
示例9: 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);
}
}
示例10: GetAssemblyInfo
public static AssemblyInfo GetAssemblyInfo(string path)
{
using (var peReader = new PEReader(new FileStream(path, FileMode.Open, FileAccess.Read)))
{
var contractReader = peReader.GetMetadataReader();
var assembly = contractReader.GetAssemblyDefinition();
var name = contractReader.GetString(assembly.Name);
var version = assembly.Version;
var references = GetAssemblyReferences(contractReader);
return new AssemblyInfo(path, name, version, references);
}
}
示例11: Execute
public override bool Execute()
{
Log.LogMessage(MessageImportance.High, "About to inspect {0} test assemblies.", TestBinaries.Length);
var perfTests = new List<ITaskItem>();
foreach (var testBinary in TestBinaries)
{
Log.LogMessage(MessageImportance.Low, "Inspecting assembly {0}.", testBinary.ItemSpec);
using (var stream = File.OpenRead(testBinary.ItemSpec))
{
using (var peFile = new PEReader(stream))
{
if(!peFile.HasMetadata){
continue;
}
var mdReader = peFile.GetMetadataReader();
foreach (var asmRefHandle in mdReader.AssemblyReferences)
{
var asmRef = mdReader.GetAssemblyReference(asmRefHandle);
var asmRefName = mdReader.GetString(asmRef.Name);
// if an assembly contains a reference to xunit.performance.core
// then it contains at least one performance test.
if (string.Compare(asmRefName, "xunit.performance.core", StringComparison.OrdinalIgnoreCase) == 0)
{
var fileName = (GetFullPaths) ? Path.GetFullPath(testBinary.ItemSpec) : Path.GetFileNameWithoutExtension(testBinary.ItemSpec);
perfTests.Add(new TaskItem(fileName));
Log.LogMessage("+ Assembly {0} contains one or more performance tests.", fileName);
break;
}
}
}
}
}
if (perfTests.Count > 0)
{
PerfTestAssemblies = perfTests.ToArray();
Log.LogMessage(MessageImportance.High, "Found {0} assemblies containing performance tests.", perfTests.Count);
}
else
{
Log.LogWarning("Didn't find any performance tests.");
}
return true;
}
示例12: BasicValidation
public void BasicValidation()
{
using (var peStream = new MemoryStream())
{
var ilBuilder = new BlobBuilder();
var metadataBuilder = new MetadataBuilder();
var entryPoint = BasicValidationEmit(metadataBuilder, ilBuilder);
WritePEImage(peStream, metadataBuilder, ilBuilder, entryPoint);
peStream.Position = 0;
var r = new PEReader(peStream);
var h = r.PEHeaders;
var mdReader = r.GetMetadataReader();
}
}
示例13: IsPublicSigned
/// <summary>
/// Returns true if the PE file meets all of the pre-conditions to be Open Source Signed.
/// Returns false and logs msbuild errors otherwise.
/// </summary>
private static bool IsPublicSigned(PEReader peReader)
{
if (!peReader.HasMetadata)
{
return false;
}
var mdReader = peReader.GetMetadataReader();
if (!mdReader.IsAssembly)
{
return false;
}
CorHeader header = peReader.PEHeaders.CorHeader;
return (header.Flags & CorFlags.StrongNameSigned) == CorFlags.StrongNameSigned;
}
示例14: 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
}
示例15: GetAssemblyAttributeValue
public static string GetAssemblyAttributeValue(string assemblyPath, string attributeName)
{
if (!File.Exists(assemblyPath))
{
return null;
}
using (var stream = File.OpenRead(assemblyPath))
using (var peReader = new PEReader(stream))
{
if (!peReader.HasMetadata)
{
return null;
}
var mdReader = peReader.GetMetadataReader();
var attrs = mdReader.GetAssemblyDefinition().GetCustomAttributes()
.Select(ah => mdReader.GetCustomAttribute(ah));
foreach (var attr in attrs)
{
var ctorHandle = attr.Constructor;
if (ctorHandle.Kind != HandleKind.MemberReference)
{
continue;
}
var container = mdReader.GetMemberReference((MemberReferenceHandle)ctorHandle).Parent;
var name = mdReader.GetTypeReference((TypeReferenceHandle)container).Name;
if (!string.Equals(mdReader.GetString(name), attributeName))
{
continue;
}
var arguments = GetFixedStringArguments(mdReader, attr);
if (arguments.Count == 1)
{
return arguments[0];
}
}
}
return null;
}