public static IEnumerable <Attribute> GetAttributes(this ClassDeclarationSyntax classDeclarationSyntax, Type attributeType) { foreach (var attribute in classDeclarationSyntax.GetAttributesSyntax(attributeType)) { yield return(attribute.GetAttribute(attributeType)); } }
public static ParsedAttribute <T> GetSingleAttribute <T>(this ClassDeclarationSyntax classDeclarationSyntax) where T : Attribute { var syntax = classDeclarationSyntax.GetAttributesSyntax <T>().ToArray(); if (syntax.Length != 1) { return(null); } return(new ParsedAttribute <T>(syntax.Single())); }
public static ParsedAttribute GetSingleAttribute(this ClassDeclarationSyntax classDeclarationSyntax, Type attributeType) { var syntax = classDeclarationSyntax.GetAttributesSyntax(attributeType).ToArray(); if (syntax.Length != 1) { return(null); } var resultType = typeof(ParsedAttribute <>).MakeGenericType(attributeType); return((ParsedAttribute)Activator.CreateInstance(resultType, syntax.Single())); }
public static ParsedAttribute[] GetMultipleAttributes(this ClassDeclarationSyntax classDeclarationSyntax, Type attributeType) { var syntax = classDeclarationSyntax.GetAttributesSyntax(attributeType).ToArray(); var resultType = typeof(ParsedAttribute <>).MakeGenericType(attributeType); var array = Array.CreateInstance(resultType, syntax.Length); for (int index = 0; index < syntax.Length; index++) { var arrayValue = Activator.CreateInstance(resultType, syntax[index]); array.SetValue(arrayValue, index); } return((ParsedAttribute[])array); }
public static ParsedAttribute <T>[] GetMultipleAttributes <T>(this ClassDeclarationSyntax classDeclarationSyntax) where T : Attribute { var syntax = classDeclarationSyntax.GetAttributesSyntax <T>().ToArray(); return(syntax.Select(s => new ParsedAttribute <T>(s)).ToArray()); }
public static IEnumerable <AttributeSyntax> GetAttributesSyntax <T>(this ClassDeclarationSyntax classDeclarationSyntax) where T : Attribute { return(classDeclarationSyntax.GetAttributesSyntax(typeof(T))); }