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


C# MetadataReader.GetCustomAttribute方法代码示例

本文整理汇总了C#中System.Reflection.Metadata.MetadataReader.GetCustomAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# MetadataReader.GetCustomAttribute方法的具体用法?C# MetadataReader.GetCustomAttribute怎么用?C# MetadataReader.GetCustomAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.Metadata.MetadataReader的用法示例。


在下文中一共展示了MetadataReader.GetCustomAttribute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetAttributeName

 /// <summary>
 /// Returns the name of the attribute class 
 /// </summary>
 internal static string GetAttributeName(MetadataReader metadataReader, CustomAttributeHandle customAttribute)
 {
     var ctorHandle = metadataReader.GetCustomAttribute(customAttribute).Constructor;
     if (ctorHandle.Kind == HandleKind.MemberReference) // MemberRef
     {
         var container = metadataReader.GetMemberReference((MemberReferenceHandle)ctorHandle).Parent;
         var name = metadataReader.GetTypeReference((TypeReferenceHandle)container).Name;
         return metadataReader.GetString(name);
     }
     else
     {
         Assert.True(false, "not impl");
         return null;
     }
 }
开发者ID:Rickinio,项目名称:roslyn,代码行数:18,代码来源:MetadataValidation.cs

示例2: 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


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