本文整理汇总了C#中INamedTypeSymbol.GetAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# INamedTypeSymbol.GetAttributes方法的具体用法?C# INamedTypeSymbol.GetAttributes怎么用?C# INamedTypeSymbol.GetAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类INamedTypeSymbol
的用法示例。
在下文中一共展示了INamedTypeSymbol.GetAttributes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AnalyzeSymbol
public override void AnalyzeSymbol(INamedTypeSymbol symbol, Compilation compilation, Action<Diagnostic> addDiagnostic, CancellationToken cancellationToken)
{
if (symbol.TypeKind != TypeKind.Enum)
{
return;
}
var flagsAttribute = WellKnownTypes.FlagsAttribute(compilation);
if (flagsAttribute == null)
{
return;
}
var zeroValuedFields = GetZeroValuedFields(symbol).ToImmutableArray();
bool hasFlagsAttribute = symbol.GetAttributes().Any(a => a.AttributeClass == flagsAttribute);
if (hasFlagsAttribute)
{
CheckFlags(symbol, zeroValuedFields, addDiagnostic);
}
else
{
CheckNonFlags(symbol, zeroValuedFields, addDiagnostic);
}
}
示例2: IsFlagsEnum
private bool IsFlagsEnum(INamedTypeSymbol typeSymbol)
{
if (typeSymbol.TypeKind != TypeKind.Enum)
{
return false;
}
foreach (var attribute in typeSymbol.GetAttributes())
{
var ctor = attribute.AttributeConstructor;
if (ctor != null)
{
var type = ctor.ContainingType;
if (!ctor.Parameters.Any() && type.Name == "FlagsAttribute")
{
var containingSymbol = type.ContainingSymbol;
if (containingSymbol.Kind == SymbolKind.Namespace &&
containingSymbol.Name == "System" &&
((INamespaceSymbol)containingSymbol.ContainingSymbol).IsGlobalNamespace)
{
return true;
}
}
}
}
return false;
}
示例3: GetApplicableAttributes
internal static IEnumerable<AttributeData> GetApplicableAttributes(INamedTypeSymbol type)
{
var attributes = new List<AttributeData>();
while (type != null)
{
attributes.AddRange(type.GetAttributes());
type = type.BaseType;
}
return attributes;
}
示例4: AnalyzeSymbol
private static void AnalyzeSymbol(INamedTypeSymbol symbol, INamedTypeSymbol attributeType, INamedTypeSymbol attributeUsageAttributeType, Action<Diagnostic> addDiagnostic)
{
if (symbol.IsAbstract || !symbol.GetBaseTypesAndThis().Contains(attributeType))
{
return;
}
bool hasAttributeUsageAttribute = symbol.GetAttributes().Any(attribute => attribute.AttributeClass.Equals(attributeUsageAttributeType));
if (!hasAttributeUsageAttribute)
{
addDiagnostic(symbol.CreateDiagnostic(Rule, symbol.Name));
}
}
示例5: AnalyzeSymbol
public override void AnalyzeSymbol(INamedTypeSymbol symbol, Compilation compilation, Action<Diagnostic> addDiagnostic, AnalyzerOptions options, CancellationToken cancellationToken)
{
if (symbol != null &&
symbol.TypeKind == TypeKind.Enum &&
symbol.DeclaredAccessibility == Accessibility.Public)
{
var flagsAttributeType = WellKnownTypes.FlagsAttribute(compilation);
if (flagsAttributeType == null)
{
return;
}
IList<ulong> memberValues;
if (DiagnosticHelpers.TryGetEnumMemberValues(symbol, out memberValues))
{
bool hasFlagsAttribute = symbol.GetAttributes().Any(a => a.AttributeClass == flagsAttributeType);
if (hasFlagsAttribute)
{
// Check "CA2217: Do not mark enums with FlagsAttribute"
IEnumerable<ulong> missingValues;
if (!ShouldBeFlags(memberValues, out missingValues))
{
Contract.ThrowIfNull(missingValues);
var missingValuesString = missingValues.Select(v => v.ToString()).Aggregate((i, j) => i + ", " + j);
var location = GetDiagnosticLocation(symbol.DeclaringSyntaxReferences[0].GetSyntax());
addDiagnostic(location.CreateDiagnostic(Rule2217, symbol.Name, missingValuesString));
}
}
else
{
// Check "CA1027: Mark enums with FlagsAttribute"
// Ignore continguous value enums to reduce noise.
if (!IsContiguous(memberValues) && ShouldBeFlags(memberValues))
{
var location = GetDiagnosticLocation(symbol.DeclaringSyntaxReferences[0].GetSyntax());
addDiagnostic(location.CreateDiagnostic(Rule1027, symbol.Name));
}
}
}
}
}
示例6: GenerateProxyInterface
public InterfaceDeclarationSyntax GenerateProxyInterface(SemanticModel semanticModel, SyntaxGenerator generator, INamedTypeSymbol sourceServiceInterface, string targetInterfaceName, Accessibility accessibility = Accessibility.Public, bool inheritServiceInterface = false, bool suppressAsyncMethodGeneration = false, bool suppressWarningComments = false)
{
SyntaxNode targetInterface = generator.InterfaceDeclaration(targetInterfaceName, accessibility: accessibility);
targetInterface = generator.AddAttributes(targetInterface, sourceServiceInterface.GetAttributes().Select(attr => generator.Attribute(attr)));
foreach (SyntaxNode method in GetOperationContractMethodDeclarations(semanticModel, generator, sourceServiceInterface, true, !inheritServiceInterface, suppressAsyncMethodGeneration))
{
targetInterface = generator.AddMembers(targetInterface, generator.AddWarningCommentIf(!suppressWarningComments, method));
}
if (inheritServiceInterface)
{
targetInterface = generator.AddInterfaceType(targetInterface, generator.TypeExpression(sourceServiceInterface));
}
targetInterface = AddGeneratedCodeAttribute(generator, targetInterface);
targetInterface = generator.AddWarningCommentIf(!suppressWarningComments, targetInterface);
return (InterfaceDeclarationSyntax)targetInterface;
}
示例7: AnalyzeSymbol
protected override void AnalyzeSymbol(INamedTypeSymbol symbol, Compilation compilation, Action<Diagnostic> addDiagnostic, AnalyzerOptions options, CancellationToken cancellationToken)
{
var attributeUsageAttribute = WellKnownTypes.AttributeUsageAttribute(compilation);
if (attributeUsageAttribute == null)
{
return;
}
if (symbol.IsAbstract || !symbol.IsAttribute())
{
return;
}
if (attributeUsageAttribute == null)
{
return;
}
var hasAttributeUsageAttribute = symbol.GetAttributes().Any(attribute => attribute.AttributeClass == attributeUsageAttribute);
if (!hasAttributeUsageAttribute)
{
addDiagnostic(symbol.CreateDiagnostic(Rule, symbol.Name));
}
}
示例8: HasCodeFix
private CodeFixStatus HasCodeFix(string diagnosticId, INamedTypeSymbol classSymbol, out string noCodeFixReason)
{
CodeFixStatus status;
noCodeFixReason = null;
var noCodeFixAttribute = classSymbol.GetAttributes().SingleOrDefault(x => x.AttributeClass == this.noCodeFixAttributeTypeSymbol);
bool hasCodeFix = noCodeFixAttribute == null;
if (!hasCodeFix)
{
status = CodeFixStatus.NotImplemented;
if (noCodeFixAttribute.ConstructorArguments.Length > 0)
{
noCodeFixReason = noCodeFixAttribute.ConstructorArguments[0].Value as string;
}
}
else
{
// Check if the code fix actually exists
hasCodeFix = this.CodeFixProviders.Any(x => x.FixableDiagnosticIds.Contains(diagnosticId));
status = hasCodeFix ? CodeFixStatus.Implemented : CodeFixStatus.NotYetImplemented;
}
return status;
}
示例9: GenerateAttributeDeclarations
private static SyntaxList<AttributeListSyntax> GenerateAttributeDeclarations(
INamedTypeSymbol namedType, CodeGenerationOptions options)
{
return AttributeGenerator.GenerateAttributeLists(namedType.GetAttributes(), options);
}
示例10: HasComSourceInterfacesAttribute
private bool HasComSourceInterfacesAttribute(INamedTypeSymbol symbol)
{
return symbol.GetAttributes().FirstOrDefault(a => a.AttributeClass == this.comSourceInterfacesAttribute) != null;
}
示例11: GetCodeGeneratorTypeNameForAttribute
private static string GetCodeGeneratorTypeNameForAttribute(INamedTypeSymbol attributeType)
{
if (attributeType != null)
{
foreach (var generatorCandidateAttribute in attributeType.GetAttributes())
{
if (generatorCandidateAttribute.AttributeClass.Name == typeof(Generators.CodeGenerationAttribute).Name)
{
return (string)generatorCandidateAttribute.ConstructorArguments.Single().Value;
}
}
}
return null;
}
示例12: GetClassAttributesWithTranslation
/// <summary>
/// Gets all the attributes from the specified <paramref name="sourceClass"/>, with special handling and translation
/// of a TemplateEventSourceAttribute which will be translated into an EventSourceAttribute with additional arguments stripped away.
/// </summary>
/// <param name="sourceClass">The clas from which to retrieve and translate attribtues.</param>
/// <returns>A list of all attributes that should be applied to the target class.</returns>
private IEnumerable<SyntaxNode> GetClassAttributesWithTranslation(INamedTypeSymbol sourceClass, EventSourceTypeInfo baseTypeInfo)
{
foreach (var attributeData in sourceClass.GetAttributes())
{
if (attributeData.AttributeClass.Name.Equals(TemplateEventSourceAttributeName))
{
yield return m_generator.Attribute(baseTypeInfo.EventSourceAttributeType.GetFullName(),
attributeData.ConstructorArguments.Select(arg => m_generator.AttributeArgument(m_generator.LiteralExpression(arg.Value)))
.Concat(
attributeData.NamedArguments.Where(arg => !typeof(GenerationOptions).GetProperties().Any(p => p.Name.Equals(arg.Key)))
.Select(arg => m_generator.AttributeArgument(arg.Key, m_generator.LiteralExpression(arg.Value.Value)))
)
);
}
else
{
yield return m_generator.Attribute(attributeData);
}
}
}
示例13: IsNonStaticClassWithNoAttributes
private static bool IsNonStaticClassWithNoAttributes(INamedTypeSymbol namedType)
{
return namedType.IsClass() &&
!namedType.IsStatic &&
!namedType.GetAttributes().Any();
}
示例14: GetContextFromClass
static RoslynReflectionAllowedContext GetContextFromClass(INamedTypeSymbol symbol)
{
RoslynReflectionAllowedContext surroundingContext = RoslynReflectionAllowedContext.None;
var surroundingClassAttributes = symbol.GetAttributes();
if (surroundingClassAttributes.Any(a => a.AttributeClass.GetFullName() == typeof(DiagnosticAnalyzerAttribute).FullName))
surroundingContext = RoslynReflectionAllowedContext.Analyzers;
else if (surroundingClassAttributes.Any(a => a.AttributeClass.GetFullName() == typeof(ExportCodeFixProviderAttribute).FullName))
surroundingContext = RoslynReflectionAllowedContext.CodeFixes;
else if (surroundingClassAttributes.Any(a => a.AttributeClass.GetFullName() == typeof(ExportCodeRefactoringProviderAttribute).FullName))
surroundingContext = RoslynReflectionAllowedContext.CodeFixes;
return surroundingContext;
}
示例15: AnalyzeSymbol
private static void AnalyzeSymbol(INamedTypeSymbol symbol, INamedTypeSymbol flagsAttributeType, Action<Diagnostic> addDiagnostic)
{
if (symbol != null &&
symbol.TypeKind == TypeKind.Enum &&
symbol.DeclaredAccessibility == Accessibility.Public)
{
IList<ulong> memberValues;
if (EnumHelpers.TryGetEnumMemberValues(symbol, out memberValues))
{
bool hasFlagsAttribute = symbol.GetAttributes().Any(a => a.AttributeClass == flagsAttributeType);
if (hasFlagsAttribute)
{
// Check "CA2217: Do not mark enums with FlagsAttribute"
IEnumerable<ulong> missingValues;
if (!ShouldBeFlags(memberValues, out missingValues))
{
Debug.Assert(missingValues != null);
string missingValuesString = missingValues.Select(v => v.ToString()).Aggregate((i, j) => i + ", " + j);
addDiagnostic(symbol.CreateDiagnostic(Rule2217, symbol.Name, missingValuesString));
}
}
else
{
// Check "CA1027: Mark enums with FlagsAttribute"
// Ignore contiguous value enums to reduce noise.
if (!IsContiguous(memberValues) && ShouldBeFlags(memberValues))
{
addDiagnostic(symbol.CreateDiagnostic(Rule1027, symbol.Name));
}
}
}
}
}