/// <summary>
        /// Verify if the attribute type can be applied to given owner symbol.
        /// Generate a diagnostic if it cannot be applied
        /// </summary>
        /// <param name="ownerSymbol">Symbol on which the attribute is applied</param>
        /// <param name="attributeType">Attribute class for the attribute</param>
        /// <param name="node">Syntax node for attribute specification</param>
        /// <param name="attributeLocation">Attribute target specifier location</param>
        /// <param name="diagnostics">Diagnostics</param>
        /// <returns>Whether attribute specification is allowed for the given symbol</returns>
        internal bool VerifyAttributeUsageTarget(Symbol ownerSymbol, NamedTypeSymbol attributeType, AttributeSyntax node, AttributeLocation attributeLocation, DiagnosticBag diagnostics)
        {
            var attributeUsageInfo = attributeType.AttributeUsageInfo(Compilation);
            if (attributeUsageInfo != null)
            {
                AttributeTargets attributeTarget;
                if (attributeLocation == AttributeLocation.Return)
                {
                    // attribute on return type
                    attributeTarget = AttributeTargets.ReturnValue;
                }
                else
                {
                    attributeTarget = ownerSymbol.GetAttributeTarget();
                }
                
                if ((attributeTarget & attributeUsageInfo.ValidTargets) != 0)
                {
                    return true;
                }

                // generate error
                Error(diagnostics, ErrorCode.ERR_AttributeOnBadSymbolType, node, GetAttributeNameFromSyntax(node), attributeUsageInfo.GetValidTargetsString());
            }

            return false;
        }
コード例 #2
0
        /// <summary>
        /// Verify if the attribute type can be applied to given owner symbol.
        /// Generate a diagnostic if it cannot be applied
        /// </summary>
        /// <param name="ownerSymbol">Symbol on which the attribute is applied</param>
        /// <param name="attributeType">Attribute class for the attribute</param>
        /// <param name="node">Syntax node for attribute specification</param>
        /// <param name="attributeLocation">Attribute target specifier location</param>
        /// <param name="diagnostics">Diagnostics</param>
        /// <returns>Whether attribute specification is allowed for the given symbol</returns>
        internal bool VerifyAttributeUsageTarget(Symbol ownerSymbol, NamedTypeSymbol attributeType, AttributeSyntax node, AttributeLocation attributeLocation, DiagnosticBag diagnostics)
        {
            var attributeUsageInfo = attributeType.AttributeUsageInfo(Compilation);

            if (attributeUsageInfo != null)
            {
                AttributeTargets attributeTarget;
                if (attributeLocation == AttributeLocation.Return)
                {
                    // attribute on return type
                    attributeTarget = AttributeTargets.ReturnValue;
                }
                else
                {
                    attributeTarget = ownerSymbol.GetAttributeTarget();
                }

                if ((attributeTarget & attributeUsageInfo.ValidTargets) != 0)
                {
                    return(true);
                }

                // generate error
                Error(diagnostics, ErrorCode.ERR_AttributeOnBadSymbolType, node, GetAttributeNameFromSyntax(node), attributeUsageInfo.GetValidTargetsString());
            }

            return(false);
        }