当前位置: 首页>>代码示例>>C#>>正文


C# MetadataReader.GetAssemblyDefinition方法代码示例

本文整理汇总了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;
        }
开发者ID:dfinke,项目名称:powershell,代码行数:61,代码来源:AssemblyMetadataHelper.cs

示例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);
        }
开发者ID:vivmishra,项目名称:dotnet-apiport,代码行数:14,代码来源:DependencyFinderEngineHelper.cs

示例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);
        }
开发者ID:MetSystem,项目名称:dotnet-apiport,代码行数:20,代码来源:DependencyFinderEngineHelper.cs

示例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")
            {
//.........这里部分代码省略.........
开发者ID:swaroop-sridhar,项目名称:corefx,代码行数:101,代码来源:FileVersionInfo.Metadata.cs

示例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);
        }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:12,代码来源:AnalyzerDependencyChecker.cs

示例6: EcmaFormatRuntimeAssembly

 private EcmaFormatRuntimeAssembly(MetadataReader reader)
 {
     AssemblyDefinition = reader.GetAssemblyDefinition();
     MetadataReader = reader;
 }
开发者ID:nattress,项目名称:corert,代码行数:5,代码来源:EcmaFormatRuntimeAssembly.cs

示例7: EcmaAssembly

 public EcmaAssembly(TypeSystemContext context, PEReader peReader, MetadataReader metadataReader)
     : base(context, peReader, metadataReader)
 {
     _assemblyDefinition = metadataReader.GetAssemblyDefinition();
 }
开发者ID:shahid-pk,项目名称:corert,代码行数:5,代码来源:EcmaAssembly.cs

示例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;
        }
开发者ID:GloryChou,项目名称:roslyn,代码行数:24,代码来源:Program.cs

示例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());
            }
        }
开发者ID:modulexcite,项目名称:dotnet-compatibility,代码行数:24,代码来源:Analyzer.cs

示例10: EcmaAssembly

 internal EcmaAssembly(TypeSystemContext context, PEReader peReader, MetadataReader metadataReader, PdbSymbolReader pdbReader)
     : base(context, peReader, metadataReader, pdbReader)
 {
     _assemblyDefinition = metadataReader.GetAssemblyDefinition();
 }
开发者ID:tijoytom,项目名称:corert,代码行数:5,代码来源:EcmaAssembly.Symbols.cs


注:本文中的System.Reflection.Metadata.MetadataReader.GetAssemblyDefinition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。