本文整理汇总了C#中System.Reflection.Metadata.MetadataReader.GetAssemblyDefinition方法的典型用法代码示例。如果您正苦于以下问题:C# MetadataReader.GetAssemblyDefinition方法的具体用法?C# MetadataReader.GetAssemblyDefinition怎么用?C# MetadataReader.GetAssemblyDefinition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Metadata.MetadataReader
的用法示例。
在下文中一共展示了MetadataReader.GetAssemblyDefinition方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetAssemblyStrongName
/// <summary>
/// Construct the strong assembly name from metadata
/// </summary>
internal static string GetAssemblyStrongName(MetadataReader metadataReader)
{
AssemblyDefinition assemblyDefinition = metadataReader.GetAssemblyDefinition();
string asmName = metadataReader.GetString(assemblyDefinition.Name);
string asmVersion = assemblyDefinition.Version.ToString();
string asmCulture = metadataReader.GetString(assemblyDefinition.Culture);
asmCulture = (asmCulture == string.Empty) ? "neutral" : asmCulture;
AssemblyHashAlgorithm hashAlgorithm = assemblyDefinition.HashAlgorithm;
BlobHandle blobHandle = assemblyDefinition.PublicKey;
BlobReader blobReader = metadataReader.GetBlobReader(blobHandle);
string publicKeyTokenString = "null";
// Extract public key token only if PublicKey exists in the metadata
if (blobReader.Length > 0)
{
byte[] publickey = blobReader.ReadBytes(blobReader.Length);
HashAlgorithm hashImpl = null;
switch (hashAlgorithm)
{
case AssemblyHashAlgorithm.Sha1:
hashImpl = SHA1.Create();
break;
case AssemblyHashAlgorithm.MD5:
hashImpl = MD5.Create();
break;
case AssemblyHashAlgorithm.Sha256:
hashImpl = SHA256.Create();
break;
case AssemblyHashAlgorithm.Sha384:
hashImpl = SHA384.Create();
break;
case AssemblyHashAlgorithm.Sha512:
hashImpl = SHA512.Create();
break;
default:
throw new NotSupportedException();
}
byte[] publicKeyHash = hashImpl.ComputeHash(publickey);
byte[] publicKeyTokenBytes = new byte[8];
// Note that, the low 8 bytes of the hash of public key in reverse order is the public key tokens.
for (int i = 1; i <= 8; i++)
{
publicKeyTokenBytes[i - 1] = publicKeyHash[publicKeyHash.Length - i];
}
// Convert bytes to hex format strings in lower case.
publicKeyTokenString = BitConverter.ToString(publicKeyTokenBytes).Replace("-", string.Empty).ToLowerInvariant();
}
string strongAssemblyName = string.Format(CultureInfo.InvariantCulture,
"{0}, Version={1}, Culture={2}, PublicKeyToken={3}",
asmName, asmVersion, asmCulture, publicKeyTokenString);
return strongAssemblyName;
}
示例2: DependencyFinderEngineHelper
public DependencyFinderEngineHelper(MetadataReader metadataReader, string assemblyPath)
{
_reader = metadataReader;
_assemblyLocation = assemblyPath;
MemberDependency = new List<MemberDependency>();
CallingAssembly = _reader.GetAssemblyInfo(assemblyPath);
// Get assembly info
var assemblyDefinition = _reader.GetAssemblyDefinition();
_currentAssemblyInfo = _reader.FormatAssemblyInfo(assemblyDefinition);
_currentAssemblyName = _reader.GetString(assemblyDefinition.Name);
}
示例3: DependencyFinderEngineHelper
public DependencyFinderEngineHelper(IDependencyFilter assemblyFilter, MetadataReader metadataReader, IAssemblyFile file)
{
_assemblyFilter = assemblyFilter;
_reader = metadataReader;
_assemblyLocation = file.Name;
MemberDependency = new List<MemberDependency>();
CallingAssembly = new AssemblyInfo
{
AssemblyIdentity = metadataReader.FormatAssemblyInfo().ToString(),
FileVersion = file.Version ?? string.Empty,
TargetFrameworkMoniker = metadataReader.GetTargetFrameworkMoniker() ?? string.Empty
};
// Get assembly info
var assemblyDefinition = _reader.GetAssemblyDefinition();
_currentAssemblyInfo = _reader.FormatAssemblyInfo(assemblyDefinition);
_currentAssemblyName = _reader.GetString(assemblyDefinition.Name);
}
示例4: LoadManagedAssemblyMetadata
/// <summary>Load our fields from the metadata of the file as represented by the provided metadata reader.</summary>
/// <param name="metadataReader">The metadata reader for the CLI file this represents.</param>
private void LoadManagedAssemblyMetadata(MetadataReader metadataReader)
{
AssemblyDefinition assemblyDefinition = metadataReader.GetAssemblyDefinition();
// Set the internal and original names based on the file name.
_internalName = _originalFilename = Path.GetFileName(_fileName);
// Set the product version based on the assembly's version (this may be overwritten
// later in the method).
Version productVersion = assemblyDefinition.Version;
_productVersion = productVersion.ToString();
_productMajor = productVersion.Major;
_productMinor = productVersion.Minor;
_productBuild = productVersion.Build != -1 ? productVersion.Build : 0;
_productPrivate = productVersion.Revision != -1 ? productVersion.Revision : 0;
// "Language Neutral" is used on Win32 for unknown language identifiers.
_language = "Language Neutral";
// Set other fields to default values in case they're not overwritten by attributes
_companyName = string.Empty;
_comments = string.Empty;
_fileDescription = " "; // this is what the managed compiler outputs when value isn't set
_fileVersion = string.Empty;
_legalCopyright = " "; // this is what the managed compiler outputs when value isn't set
_legalTrademarks = string.Empty;
_productName = string.Empty;
_privateBuild = string.Empty;
_specialBuild = string.Empty;
// Be explicit about initialization to suppress warning about fields not being set
_isDebug = false;
_isPatched = false;
_isPreRelease = false;
_isPrivateBuild = false;
_isSpecialBuild = false;
bool sawAssemblyInformationalVersionAttribute = false;
// Everything else is parsed from assembly attributes
MetadataStringComparer comparer = metadataReader.StringComparer;
foreach (CustomAttributeHandle attrHandle in assemblyDefinition.GetCustomAttributes())
{
CustomAttribute attr = metadataReader.GetCustomAttribute(attrHandle);
StringHandle typeNamespaceHandle = default(StringHandle), typeNameHandle = default(StringHandle);
if (TryGetAttributeName(metadataReader, attr, out typeNamespaceHandle, out typeNameHandle) &&
comparer.Equals(typeNamespaceHandle, "System.Reflection"))
{
if (comparer.Equals(typeNameHandle, "AssemblyCompanyAttribute"))
{
GetStringAttributeArgumentValue(metadataReader, attr, ref _companyName);
}
else if (comparer.Equals(typeNameHandle, "AssemblyCopyrightAttribute"))
{
GetStringAttributeArgumentValue(metadataReader, attr, ref _legalCopyright);
}
else if (comparer.Equals(typeNameHandle, "AssemblyDescriptionAttribute"))
{
GetStringAttributeArgumentValue(metadataReader, attr, ref _comments);
}
else if (comparer.Equals(typeNameHandle, "AssemblyFileVersionAttribute"))
{
GetStringAttributeArgumentValue(metadataReader, attr, ref _fileVersion);
ParseVersion(_fileVersion, out _fileMajor, out _fileMinor, out _fileBuild, out _filePrivate);
}
else if (comparer.Equals(typeNameHandle, "AssemblyInformationalVersionAttribute"))
{
GetStringAttributeArgumentValue(metadataReader, attr, ref _productVersion);
ParseVersion(_productVersion, out _productMajor, out _productMinor, out _productBuild, out _productPrivate);
sawAssemblyInformationalVersionAttribute = true;
}
else if (comparer.Equals(typeNameHandle, "AssemblyProductAttribute"))
{
GetStringAttributeArgumentValue(metadataReader, attr, ref _productName);
}
else if (comparer.Equals(typeNameHandle, "AssemblyTrademarkAttribute"))
{
GetStringAttributeArgumentValue(metadataReader, attr, ref _legalTrademarks);
}
else if (comparer.Equals(typeNameHandle, "AssemblyTitleAttribute"))
{
GetStringAttributeArgumentValue(metadataReader, attr, ref _fileDescription);
}
}
}
// When the managed compiler sees an [AssemblyVersion(...)] attribute, it uses that to set
// both the assembly version and the product version in the Win32 resources. If it doesn't
// see an [AssemblyVersion(...)], then it sets the assembly version to 0.0.0.0, however it
// sets the product version in the Win32 resources to whatever was defined in the
// [AssemblyFileVersionAttribute(...)] if there was one (unless there is an AssemblyInformationalVersionAttribute,
// in which case it always uses that for the product version). Without parsing the Win32 resources,
// we can't differentiate these two cases, so given the rarity of explicitly setting an
// assembly's version number to 0.0.0.0, we assume that if it is 0.0.0.0 then the attribute
// wasn't specified and we use the file version.
if (!sawAssemblyInformationalVersionAttribute && _productVersion == "0.0.0.0")
{
//.........这里部分代码省略.........
示例5: ReadAssemblyIdentity
private static AssemblyIdentity ReadAssemblyIdentity(SystemMetadataReader metadataReader)
{
var assemblyDefinition = metadataReader.GetAssemblyDefinition();
string name = metadataReader.GetString(assemblyDefinition.Name);
Version version = assemblyDefinition.Version;
string cultureName = metadataReader.GetString(assemblyDefinition.Culture);
ImmutableArray<byte> publicKeyOrToken = metadataReader.GetBlobContent(assemblyDefinition.PublicKey);
AssemblyFlags flags = assemblyDefinition.Flags;
bool hasPublicKey = (flags & AssemblyFlags.PublicKey) != 0;
return new AssemblyIdentity(name, version, cultureName, publicKeyOrToken, hasPublicKey: hasPublicKey);
}
示例6: EcmaFormatRuntimeAssembly
private EcmaFormatRuntimeAssembly(MetadataReader reader)
{
AssemblyDefinition = reader.GetAssemblyDefinition();
MetadataReader = reader;
}
示例7: EcmaAssembly
public EcmaAssembly(TypeSystemContext context, PEReader peReader, MetadataReader metadataReader)
: base(context, peReader, metadataReader)
{
_assemblyDefinition = metadataReader.GetAssemblyDefinition();
}
示例8: 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, MetadataReader mdReader, bool unSign)
{
CorHeader header = peReader.PEHeaders.CorHeader;
var expectedStrongNameFlag = unSign ? CorFlags.StrongNameSigned : 0;
var actualStrongNameFlag = header.Flags & CorFlags.StrongNameSigned;
if (expectedStrongNameFlag != actualStrongNameFlag)
{
Console.Error.WriteLine($"PE file is {(unSign ? "not" : "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;
}
示例9: CheckAssemblyProperties
private void CheckAssemblyProperties(MetadataReader referenceMetadata, MetadataReader newMetadata)
{
AssemblyDefinition referenceAssemblyDefinition = referenceMetadata.GetAssemblyDefinition();
AssemblyDefinition newAssemblyDefinition = newMetadata.GetAssemblyDefinition();
string referenceName = referenceMetadata.GetString(referenceAssemblyDefinition.Name);
string newName = newMetadata.GetString(newAssemblyDefinition.Name);
if (!string.Equals(referenceName, newName, StringComparison.Ordinal))
_logger.Report(AssemblyNameMustNotBeChanged.CreateMessage());
string referenceCulture = referenceMetadata.GetString(referenceAssemblyDefinition.Culture);
string newCulture = referenceMetadata.GetString(newAssemblyDefinition.Culture);
if (!string.Equals(referenceCulture, newCulture, StringComparison.Ordinal))
throw new NotImplementedException("Assembly culture changed.");
if (!referenceAssemblyDefinition.PublicKey.IsNil)
{
// adding a public key is supported, but removing or changing it is not.
var referencePublicKey = referenceMetadata.GetBlobContent(referenceAssemblyDefinition.PublicKey);
var newPublicKey = newMetadata.GetBlobContent(newAssemblyDefinition.PublicKey);
if (!referencePublicKey.SequenceEqual(newPublicKey))
_logger.Report(PublicKeyMustNotBeChanged.CreateMessage());
}
}
示例10: EcmaAssembly
internal EcmaAssembly(TypeSystemContext context, PEReader peReader, MetadataReader metadataReader, PdbSymbolReader pdbReader)
: base(context, peReader, metadataReader, pdbReader)
{
_assemblyDefinition = metadataReader.GetAssemblyDefinition();
}