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
codeObjectobjectA 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
parentSyntaxNodeThe parent syntax node to search within.
childSyntaxNodeThe child syntax node to find.
isOnlyDirectSubboolWhether 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
parentSyntaxNodeThe parent syntax node to search within.
childSyntaxNodeThe child syntax node to find.
isOnlyDirectSubboolWhether to search only direct child nodes or use span-based matching.
foundIndexintThe 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
slnPathstringThe file path to the solution (.sln) file.
isSkippingUnrecognizedProjectsboolWhether to skip projects that cannot be recognized by MSBuild.
Returns
GetClass(SyntaxNode)
Gets the first class declaration from the given root syntax node.
public static ClassDeclarationSyntax? GetClass(SyntaxNode root)
Parameters
rootSyntaxNodeThe 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
rootNodeSyntaxNodeThe root syntax node (typically CompilationUnitSyntax).
namespaceNodeSyntaxNodeOutputs 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
methodDeclarationMethodDeclarationSyntaxThe method declaration syntax node.
alsoModifierboolWhether 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
codestringThe C# code to parse.
isWrappingIntoClassboolWhether 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
assemblyAssemblyThe assembly to search for types.
containsstringThe substring to match against type names.
Returns
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
syntaxNodesIList<SyntaxNode>The list of syntax nodes (must be MethodDeclarationSyntax).
alsoModifierboolWhether to include access modifiers in the header.
Returns
IsStatic(SyntaxTokenList)
Determines whether the given modifier list contains the static keyword.
public static bool IsStatic(SyntaxTokenList modifiers)
Parameters
modifiersSyntaxTokenListThe 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
namestringThe 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
classDeclarationClassDeclarationSyntaxThe class declaration to remove the node from.
nodeToRemoveSyntaxNodeThe syntax node to remove.
keepDirectivesSyntaxRemoveOptionsOptions 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
originalNodeSyntaxNodeThe original syntax node to replace.
replacementNodeSyntaxNodeThe replacement syntax node.
rootSyntaxNodeOutputs 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
originalNodeSyntaxNodeThe original syntax node to replace.
replacementNodeSyntaxNodeThe replacement syntax node.
rootSyntaxNodeOutputs the new root syntax node after replacement.
Returns
- T
The replacement node cast to
T.
Type Parameters
TThe 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
syntaxNodeSyntaxNodeThe 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
codestringThe C# code to wrap.
Returns
- string
The code wrapped in a class declaration block.