Table of Contents

Class RoslynHelper

Namespace
SunamoRoslyn
Assembly
SunamoRoslyn.dll

Provides helper methods for working with Roslyn syntax trees, including variable analysis, project loading, code wrapping, and syntax node operations.

public class RoslynHelper
Inheritance
RoslynHelper
Inherited Members
Extension Methods

Methods

AddWhereIsUsedVariablesInMethods(object)

Analyzes global variables in the given code and adds XML documentation comments listing which methods use each variable. The argument can be a SyntaxNode or a string of C# code.

public static string AddWhereIsUsedVariablesInMethods(object codeObject)

Parameters

codeObject object

A SyntaxNode or string representing the C# code to analyze.

Returns

string

The modified source code with usage documentation added to global variables.

FindNode(SyntaxNode, SyntaxNode, bool)

Finds a syntax node within a parent that matches the given child node, searching only direct children.

public static SyntaxNode? FindNode(SyntaxNode parent, SyntaxNode child, bool isOnlyDirectSub)

Parameters

parent SyntaxNode

The parent syntax node to search within.

child SyntaxNode

The child syntax node to find.

isOnlyDirectSub bool

Whether to search only direct child nodes.

Returns

SyntaxNode

The found syntax node, or null if not found.

FindNode(SyntaxNode, SyntaxNode, bool, out int)

Finds a syntax node within a parent that matches the given child node. When searching in classes, insert the class as the parent. If the root/namespace is inserted, the method will return the whole class because it contains the method.

public static SyntaxNode? FindNode(SyntaxNode parent, SyntaxNode child, bool isOnlyDirectSub, out int foundIndex)

Parameters

parent SyntaxNode

The parent syntax node to search within.

child SyntaxNode

The child syntax node to find.

isOnlyDirectSub bool

Whether to search only direct child nodes or use span-based matching.

foundIndex int

The index of the found node within its parent's members.

Returns

SyntaxNode

The found syntax node, or null if not found.

GetAllProjectsInSolution(string, bool)

Returns all projects in the specified solution, including referenced projects. If you want only projects listed directly in the .sln file, use AP.GetProjectsInSlnFile instead.

public static Task<List<Project>> GetAllProjectsInSolution(string slnPath, bool isSkippingUnrecognizedProjects = false)

Parameters

slnPath string

The file path to the solution (.sln) file.

isSkippingUnrecognizedProjects bool

Whether to skip projects that cannot be recognized by MSBuild.

Returns

Task<List<Project>>

A list of all projects in the solution.

GetClass(SyntaxNode)

Gets the first class declaration from the given root syntax node.

public static ClassDeclarationSyntax? GetClass(SyntaxNode root)

Parameters

root SyntaxNode

The root syntax node to search.

Returns

ClassDeclarationSyntax

The class declaration syntax node, or null if multiple classes exist.

GetClass(SyntaxNode, out SyntaxNode?)

Gets the first class declaration from a syntax tree root, also outputting the namespace node. Returns null if more than one class declaration exists at the top level. The root should be a CompilationUnitSyntax rather than a plain SyntaxNode because Members on a SyntaxNode via ChildNodes includes usings.

public static ClassDeclarationSyntax? GetClass(SyntaxNode rootNode, out SyntaxNode? namespaceNode)

Parameters

rootNode SyntaxNode

The root syntax node (typically CompilationUnitSyntax).

namespaceNode SyntaxNode

Outputs the namespace syntax node, or null if the class is at root level.

Returns

ClassDeclarationSyntax

The class declaration syntax node, or null if multiple classes exist.

GetHeaderOfMethod(MethodDeclarationSyntax, bool)

Builds a method header string from a method declaration syntax node, including optional access modifiers, static keyword, return type, name, and parameters.

public static string GetHeaderOfMethod(MethodDeclarationSyntax methodDeclaration, bool alsoModifier = true)

Parameters

methodDeclaration MethodDeclarationSyntax

The method declaration syntax node.

alsoModifier bool

Whether to include access modifiers in the header.

Returns

string

The method header string.

GetSyntaxTree(string, bool)

Parses the given C# code into a syntax tree. The code must start with a class or namespace declaration unless wrapped.

public static SyntaxTree GetSyntaxTree(string code, bool isWrappingIntoClass = false)

Parameters

code string

The C# code to parse.

isWrappingIntoClass bool

Whether to wrap the code in a dummy class before parsing.

Returns

SyntaxTree

The parsed syntax tree.

GetTypesInAssembly(Assembly, string)

Returns all types in the specified assembly whose names contain the given substring.

public static List<Type> GetTypesInAssembly(Assembly assembly, string contains)

Parameters

assembly Assembly

The assembly to search for types.

contains string

The substring to match against type names.

Returns

List<Type>

A list of types whose names contain the specified substring.

HeadersOfMethod(IList<SyntaxNode>, bool)

Returns the method header strings for a list of method declaration syntax nodes.

public static List<string> HeadersOfMethod(IList<SyntaxNode> syntaxNodes, bool alsoModifier = true)

Parameters

syntaxNodes IList<SyntaxNode>

The list of syntax nodes (must be MethodDeclarationSyntax).

alsoModifier bool

Whether to include access modifiers in the header.

Returns

List<string>

A list of method header strings.

IsStatic(SyntaxTokenList)

Determines whether the given modifier list contains the static keyword.

public static bool IsStatic(SyntaxTokenList modifiers)

Parameters

modifiers SyntaxTokenList

The list of syntax tokens representing modifiers.

Returns

bool

True if the modifiers include the static keyword; otherwise, false.

NameWithoutGeneric(string)

Removes the generic type parameter portion from a type name.

public static string NameWithoutGeneric(string name)

Parameters

name string

The type name potentially containing generic parameters.

Returns

string

The type name without the generic portion.

RemoveNode(ClassDeclarationSyntax, SyntaxNode, SyntaxRemoveOptions)

Removes a syntax node from a class declaration. Currently not implemented.

public static ClassDeclarationSyntax? RemoveNode(ClassDeclarationSyntax classDeclaration, SyntaxNode nodeToRemove, SyntaxRemoveOptions keepDirectives)

Parameters

classDeclaration ClassDeclarationSyntax

The class declaration to remove the node from.

nodeToRemove SyntaxNode

The syntax node to remove.

keepDirectives SyntaxRemoveOptions

Options controlling how directives and trivia are handled during removal.

Returns

ClassDeclarationSyntax

The modified class declaration.

ReplaceNode(SyntaxNode, SyntaxNode, out SyntaxNode)

Replaces a syntax node in the tree and walks up to the root, returning the new root. After calling this method, the caller must reassign the result because the old references are invalidated.

public static void ReplaceNode(SyntaxNode originalNode, SyntaxNode replacementNode, out SyntaxNode root)

Parameters

originalNode SyntaxNode

The original syntax node to replace.

replacementNode SyntaxNode

The replacement syntax node.

root SyntaxNode

Outputs the new root syntax node after replacement.

ReplaceNode<T>(SyntaxNode, SyntaxNode, out SyntaxNode)

Replaces a syntax node in the tree and walks up to the root, returning both the replaced node (cast to T) and the new root. CompilationUnitSyntax is also a SyntaxNode. After calling this method, the caller must reassign the result because old references are invalidated.

public static T? ReplaceNode<T>(SyntaxNode originalNode, SyntaxNode replacementNode, out SyntaxNode root) where T : SyntaxNode

Parameters

originalNode SyntaxNode

The original syntax node to replace.

replacementNode SyntaxNode

The replacement syntax node.

root SyntaxNode

Outputs the new root syntax node after replacement.

Returns

T

The replacement node cast to T.

Type Parameters

T

The type of the replacement node to return.

WithoutAllTrivia(SyntaxNode)

Removes all leading and trailing trivia from a syntax node.

public static SyntaxNode WithoutAllTrivia(SyntaxNode syntaxNode)

Parameters

syntaxNode SyntaxNode

The syntax node to strip trivia from.

Returns

SyntaxNode

The syntax node without any leading or trailing trivia.

WrapIntoClass(string)

Wraps the given code snippet inside a dummy class declaration.

public static string WrapIntoClass(string code)

Parameters

code string

The C# code to wrap.

Returns

string

The code wrapped in a class declaration block.