Utilities for easy implementation of source generators. A big thanks to TheArchitectDev for providing many of these extensions.
Check out CodeChops projects for more projects.
- TryGetArguments: Tries to get the arguments of the attribute.
- GetArgumentOrDefault: Gets the value of the attribute argument, or the (provided) default value if the argument was not provided.
- TryGetArgument: Tries to get the value of the attribute argument.
- IsInSystemNamespace**: Returns whether the given
INamedTypeSymbol
is or resides in theSystem
namespace. - HasFullName**: Returns whether the given
INamedTypeSymbol
has the given 'fullName'.
- HasAttributeName: Checks if a name syntax has a specific attribute of provided 'expectedName'.
- ExtractAttributeName: Extracts the attribute name from the name syntax.
- TrimEnd: Trims the end of a
StringBuilder
. - AppendLine: Appends the
StringBuilder
with the provided text retriever (Func<string?>
) and adds a newline if the resulting text is notnull
.
- ToTitleCase: Returns the input with the first character converted to uppercase.
- NormalizeWhitespace**: Normalizes the whitespace for the given C# source code as much as possible.
- GetStableHashCode32**: Gets a stable hash code (
int
). - GetStableHashCode64**: Gets a stable hash code (
ulong
). - GetStableStringHashCode32**: Gets a stable
int
hash code asstring
. - GetStableStringHashCode64**: Gets a stable
ulong
hash code asstring
. - ToBase32Chars8**: Converts the given 8
bytes
to 13 base32chars
. - Write: Returns specific text when the provided value is not
null
.
- GetClassGenericConstraints: Gets the generic constraints of a
TypeDeclarationSyntax
. - GetUsings: Gets the
usings
of aTypeDeclarationSyntax
.
- IsType**: Returns whether the
ITypeSymbol
is of a specific type. - IsOrInheritsClass**: Returns whether the
ITypeSymbol
is or inherits from a certainclass
, as determined by the given predicate. - IsOrImplementsInterface**: Returns whether the
ITypeSymbol
is or implements a certaininterface
, as determined by the given predicate. - HasSingleGenericTypeArgument**: Returns whether the is a constructed generic type with a single type argument matching the provided 'requiredTypeArgument'.
- IsNumeric: Returns whether the represents an numeric type, such as an
int
orulong
. - IsGeneric**: Returns whether the is a generic type (with the given number of type parameters).
- IsNullable**: Returns whether the is a
Nullable<T>
. - IsSelfEquatable**: Returns whether the given implements
IEquatable{T}
against itself. - IsComparable**: Returns whether the implements any
IComparable
or interface. - IsEnumerable**: Returns whether the is or implements
System.Collections.IEnumerable
. - HasEqualsOverride**: Returns whether the or a base type has an override of more specific than
Object
's implementation. - HasAttribute**: Returns whether the is annotated with the specified attribute(s).
- HasConversionTo**: Returns whether the defines a conversion to the specified type.
- HasConversionFrom**: Returns whether the defines a conversion from the specified type.
- GetAvailableConversionsFromNatives**: Enumerates the native types (
string
,int
,bool
,decimal
,double
,char
, etc.) from which the given is convertible. - CreateStringExpression**: Returns the code for a string expression of the given of "this".
- CreateComparisonExpression**: Returns the code for a comparison expression on the given 'memberName' between "this" and "other".
- GetFullTypeNameWithoutGenericParameters: Converts names like
string
toglobal::System.String
excluding generic parameter names (if used). - GetFullTypeNameWithGenericParameters: Converts names like
string
toglobal::System.String
including generic parameter names (if used). - GetTypeNameWithGenericParameters: Gets the type name including generic parameters (without
namespace
). - GetTypeKindName: Gets the name of the TypeKind. For example: a
record class
will be 'record class', aninterface
will be 'interface'. - GetObjectDeclaration: Gets the declaration of a
class
,record
orinterface
. E.g.: 'public abstract (partial) class Test'.
- GetFileName: Gets a unique source generation file name (takes namespaces and generic parameters into account).
- Removes 'global::' from the start of the name if it exists.
- Replaces invalid file name characters to '_'.
- Removes the root namespace of the source assembly from the start file name.
- Adds suffix '.g.cs' to the file name, if needed.
- GetNameWithoutGenerics: Gets the name without generic parameters.
- HasGenericParameter: Checks if a name contains a generic parameter.
- GetNameWithGenerics: Gets the name of a class including the generic types. E.g:
System.Collections.Generic.Dictionary'2
becomesSystem.Collections.Generic.Dictionary<System.String,System.Object>
.
** Has been provided by TheArchitectDev.