Skip to content

SymbolExtensions

Extension methods for working with symbols.

C# Signature
internal static class SymbolExtensions

The following example checks a symbol for an attribute and gets its declaration string.

C#
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);
}
}

Gets the type declaration, without access modifiers, for the provided symbol.

If isPartial is true then the type declaration will include the partial keyword.

C# Signature
public static string GetDeclarationString(this ITypeSymbol symbol, bool isPartial);

The symbol to get the declaration from.

Whether the type declaration should include the partial keyword.

The following example shows how to get the declaration string of a symbol.

C#
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.
}
}

The type declaration.

Determines whether the given symbol has the specified attribute.

C# Signature
public static bool HasAttribute(this ISymbol symbol, string fullAttributeTypeName);

The symbol to check.

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.

true if the symbol has the specified attribute; otherwise false.

Determines whether the given symbol has the specified attribute.

C# Signature
public static bool HasAttribute(this ISymbol symbol, ReadOnlySpan<char> fullAttributeTypeName);

The symbol to check.

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.

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.

C# Signature
public static bool TryGetAttribute(this ISymbol symbol, string fullAttributeTypeName, out AttributeData? attribute);

The symbol to check.

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.

When this method returns, contains the attribute with the specified name, if the attribute was found; otherwise, null. This parameter is passed uninitialized.

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.

C# Signature
public static bool TryGetAttribute(this ISymbol symbol, ReadOnlySpan<char> fullAttributeTypeName, out AttributeData? attribute);

The symbol to check.

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.

When this method returns, contains the attribute with the specified name, if the attribute was found; otherwise, null. This parameter is passed uninitialized.

true if the symbol has the specified attribute; otherwise false.