Last active
January 28, 2024 06:25
-
-
Save frankbryce/a4ee2bf799ab3878ae91 to your computer and use it in GitHub Desktop.
This is a factory to create a TypeSyntax Roslyn Model object from simple strings. Enjoy!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using Microsoft.CodeAnalysis.CSharp; | |
using Microsoft.CodeAnalysis.CSharp.Syntax; | |
namespace StackOverflow.Roslyn | |
{ | |
public static class TypeSyntaxFactory | |
{ | |
/// <summary> | |
/// Used to generate a type without generic arguments | |
/// </summary> | |
/// <param name="identifier">The name of the type to be generated</param> | |
/// <returns>An instance of TypeSyntax from the Roslyn Model</returns> | |
public static TypeSyntax GetTypeSyntax(string identifier) | |
{ | |
return | |
SyntaxFactory.IdentifierName( | |
SyntaxFactory.Identifier(identifier) | |
); | |
} | |
/// <summary> | |
/// Used to generate a type with generic arguments | |
/// </summary> | |
/// <param name="identifier">Name of the Generic Type</param> | |
/// <param name="arguments"> | |
/// Types of the Generic Arguments, which must be basic identifiers | |
/// </param> | |
/// <returns>An instance of TypeSyntax from the Roslyn Model</returns> | |
public static TypeSyntax GetTypeSyntax(string identifier, params string[] arguments) | |
{ | |
return GetTypeSyntax(identifier, arguments.Select(GetTypeSyntax).ToArray()); | |
} | |
/// <summary> | |
/// Used to generate a type with generic arguments | |
/// </summary> | |
/// <param name="identifier">Name of the Generic Type</param> | |
/// <param name="arguments"> | |
/// Types of the Generic Arguments, which themselves may be generic types | |
/// </param> | |
/// <returns>An instance of TypeSyntax from the Roslyn Model</returns> | |
public static TypeSyntax GetTypeSyntax(string identifier, params TypeSyntax[] arguments) | |
{ | |
return | |
SyntaxFactory.GenericName( | |
SyntaxFactory.Identifier(identifier), | |
SyntaxFactory.TypeArgumentList( | |
SyntaxFactory.SeparatedList( | |
arguments.Select( | |
x => | |
{ | |
if(x is GenericNameSyntax) | |
{ | |
var gen_x = x as GenericNameSyntax; | |
return | |
GetTypeSyntax( | |
gen_x.Identifier.ToString(), | |
gen_x.TypeArgumentList.Arguments.ToArray() | |
); | |
} | |
else | |
{ | |
return x; | |
} | |
} | |
) | |
) | |
) | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment