SymbolExtensions
Extension methods for working with symbols.
internal static class SymbolExtensionsExamples
Section titled “Examples”The following example checks a symbol for an attribute and gets its declaration string.
using System;using Hertzole.SourceGen;using Microsoft.CodeAnalysis;
public static class SymbolInspector{ public static void Inspect(INamedTypeSymbol symbol) { // Checks for the fully qualified attribute name. if (symbol.HasAttribute("My.Namespace.MyAttribute")) { Console.WriteLine("The type has the attribute."); }
// Gets the attribute data if the attribute exists. if (symbol.TryGetAttribute("My.Namespace.MyAttribute", out AttributeData? attribute)) { Console.WriteLine(attribute); }
// Returns e.g. "partial class" for a partial class symbol. string declaration = symbol.GetDeclarationString(isPartial: true); Console.WriteLine(declaration); }}Methods
Section titled “Methods”GetDeclarationString(ITypeSymbol, bool)
Section titled “GetDeclarationString(ITypeSymbol, bool)”Gets the type declaration, without access modifiers, for the provided symbol.
If isPartial is true then the type declaration will include the partial keyword.
public static string GetDeclarationString(this ITypeSymbol symbol, bool isPartial);symbol
Section titled “symbol”The symbol to get the declaration from.
isPartial
Section titled “isPartial”Whether the type declaration should include the partial keyword.
Examples
Section titled “Examples”The following example shows how to get the declaration string of a symbol.
using Hertzole.SourceGen;using Microsoft.CodeAnalysis;
internal static class CodeHelper{ public static void AppendDeclaration(CodeWriter writer, ITypeSymbol symbol, bool isPartial) { writer.Append(symbol.GetDeclarationString(isPartial));
// If the symbol is... // an interface -> "interface" // a class -> "class" // a static class -> "static class" // a record -> "record" // a struct -> "struct" // a readonly struct -> "readonly struct" // a record struct -> "record struct" // a readonly record struct -> "readonly record struct" // If partial is true, then the declaration will include the "partial" keyword. }}Returns
Section titled “Returns”The type declaration.
HasAttribute(ISymbol, string)
Section titled “HasAttribute(ISymbol, string)”Determines whether the given symbol has the specified attribute.
public static bool HasAttribute(this ISymbol symbol, string fullAttributeTypeName);symbol
Section titled “symbol”The symbol to check.
fullAttributeTypeName
Section titled “fullAttributeTypeName”The name of the attribute to check for. It needs to include the full namespace and type name. E.g My.Namespace.MyAttribute. It may also include the global:: prefix.
Returns
Section titled “Returns”true if the symbol has the specified attribute; otherwise false.
HasAttribute(ISymbol, ReadOnlySpan<char>)
Section titled “HasAttribute(ISymbol, ReadOnlySpan<char>)”Determines whether the given symbol has the specified attribute.
public static bool HasAttribute(this ISymbol symbol, ReadOnlySpan<char> fullAttributeTypeName);symbol
Section titled “symbol”The symbol to check.
fullAttributeTypeName
Section titled “fullAttributeTypeName”The name of the attribute to check for. It needs to include the full namespace and type name. E.g My.Namespace.MyAttribute. It may also include the global:: prefix.
Returns
Section titled “Returns”true if the symbol has the specified attribute; otherwise false.
TryGetAttribute(ISymbol, string, out AttributeData?)
Section titled “TryGetAttribute(ISymbol, string, out AttributeData?)”Determines whether the given symbol has the specified attribute, and returns the attribute if it does.
public static bool TryGetAttribute(this ISymbol symbol, string fullAttributeTypeName, out AttributeData? attribute);symbol
Section titled “symbol”The symbol to check.
fullAttributeTypeName
Section titled “fullAttributeTypeName”The name of the attribute to check for. It needs to include the full namespace and type name. E.g My.Namespace.MyAttribute. It may also include the global:: prefix.
attribute
Section titled “attribute”When this method returns, contains the attribute with the specified name, if the attribute was found; otherwise, null. This parameter is passed uninitialized.
Returns
Section titled “Returns”true if the symbol has the specified attribute; otherwise false.
TryGetAttribute(ISymbol, ReadOnlySpan<char>, out AttributeData?)
Section titled “TryGetAttribute(ISymbol, ReadOnlySpan<char>, out AttributeData?)”Determines whether the given symbol has the specified attribute, and returns the attribute if it does.
public static bool TryGetAttribute(this ISymbol symbol, ReadOnlySpan<char> fullAttributeTypeName, out AttributeData? attribute);symbol
Section titled “symbol”The symbol to check.
fullAttributeTypeName
Section titled “fullAttributeTypeName”The name of the attribute to check for. It needs to include the full namespace and type name. E.g My.Namespace.MyAttribute. It may also include the global:: prefix.
attribute
Section titled “attribute”When this method returns, contains the attribute with the specified name, if the attribute was found; otherwise, null. This parameter is passed uninitialized.
Returns
Section titled “Returns”true if the symbol has the specified attribute; otherwise false.