Table of Contents

Class CSharpHelper

Namespace
SunamoDevCode.SunamoCSharp
Assembly
SunamoDevCode.dll

Provides static helper methods for C# code analysis, generation, and manipulation.

public static class CSharpHelper
Inheritance
CSharpHelper
Inherited Members

Fields

Import

JavaScript/TypeScript import keyword prefix.

public const string Import = "import "

Field Value

string

Using

C# using keyword prefix.

public const string Using = "using "

Field Value

string

Methods

AddTypeToEveryFile(string)

Adds a static Type field (for ThrowEx) to every C# file that uses ThrowEx but lacks the type definition.

public static Task AddTypeToEveryFile(string BasePathsHelperVs)

Parameters

BasePathsHelperVs string

Base path to the Visual Studio projects folder.

Returns

Task

CreateConsts(Dictionary<string, string>)

Generates C# const field declarations from a dictionary of name-value pairs.

public static string CreateConsts(Dictionary<string, string> dict)

Parameters

dict Dictionary<string, string>

Dictionary where keys are constant names and values are constant values.

Returns

string

Generated C# code with const declarations.

CreateConstsForSearchUris(List<string>)

Generates C# const field declarations and a list for a collection of search URIs.

public static string CreateConstsForSearchUris(List<string> uris)

Parameters

uris List<string>

List of URI strings to generate constants from.

Returns

string

Generated C# code with const declarations and an aggregated list.

DefaultValueForTypeObject(string)

Returns the default value for a C# type as an object.

public static object DefaultValueForTypeObject(string type)

Parameters

type string

C# type name or full name to get the default for.

Returns

object

Default value for the given type.

DefaultValueForTypeSqLite(string)

Returns the default value string for a SQLite data type.

public static string DefaultValueForTypeSqLite(string type)

Parameters

type string

SQLite type name (TEXT, INTEGER, REAL, DATETIME, BLOB).

Returns

string

Default value string for the given type.

DictionaryWithClass<Key, Value>(int, string, Dictionary<Key, Value>, CSharpGeneratorArgs?)

Generates a C# class containing a dictionary initialized from an existing dictionary. AddingValue defaults to false.

public static string DictionaryWithClass<Key, Value>(int tabCount, string nameDictionary, Dictionary<Key, Value> data, CSharpGeneratorArgs? argument = null) where Key : notnull

Parameters

tabCount int

Number of tabs for indentation.

nameDictionary string

Name for the generated class and dictionary.

data Dictionary<Key, Value>

Source dictionary to generate code from.

argument CSharpGeneratorArgs

Optional code generation arguments.

Returns

string

Generated C# code for the class with dictionary initialization.

Type Parameters

Key

Type of dictionary keys.

Value

Type of dictionary values.

DictionaryWithClass<Key, Value>(int, string, List<Key>, Func<Value>, CSharpGeneratorArgs?)

Generates a C# class containing a dictionary initialized with random values for each key.

public static string DictionaryWithClass<Key, Value>(int tabCount, string nameDictionary, List<Key> keys, Func<Value> randomValue, CSharpGeneratorArgs? argument = null) where Key : notnull

Parameters

tabCount int

Number of tabs for indentation.

nameDictionary string

Name for the generated class and dictionary.

keys List<Key>

List of keys for the dictionary.

randomValue Func<Value>

Function that generates a random value for each entry.

argument CSharpGeneratorArgs

Optional code generation arguments.

Returns

string

Generated C# code for the class with dictionary initialization.

Type Parameters

Key

Type of dictionary keys.

Value

Type of dictionary values.

GenerateConstants(int, Func<string, string>, List<string>)

GenerateConstants - const without value GetConsts - static readonly with value

public static string GenerateConstants(int tabCount, Func<string, string> changeInput, List<string> input)

Parameters

tabCount int
changeInput Func<string, string>
input List<string>

Returns

string

GenerateProperties(GeneratePropertiesArgs)

Generates C# property declarations with backing fields from the given arguments.

public static string GenerateProperties(GeneratePropertiesArgs argument)

Parameters

argument GeneratePropertiesArgs

Arguments specifying input lines and generation options.

Returns

string

Generated property code.

GetArray(List<string>, string)

Return also List because Array isn't use

public static string GetArray(List<string> input, string arrayName)

Parameters

input List<string>
arrayName string

Returns

string

GetConsts(List<string>, List<string>, bool?)

Generates C# static readonly string constant declarations from the given names and values.

public static string GetConsts(List<string> names, List<string> list, bool? toCamelConventionFirstCharLower)

Parameters

names List<string>

Optional list of custom constant names (can be null to use values as names).

list List<string>

List of values for the constants.

toCamelConventionFirstCharLower bool?

Whether to apply camelCase convention to constant names (null = no conversion).

Returns

string

Generated C# code with constant declarations.

GetConsts(List<string>, List<string>, bool?, Type)

A1 can be null

A3 null = not use Pascal convention

GenerateConstants - const without value GetConsts - static readonly with value

public static string GetConsts(List<string> names, List<string> list, bool? toCamelConventionFirstCharLower, Type type)

Parameters

names List<string>

Optional list of custom constant names (can be null to use values as names).

list List<string>

List of values for the constants.

toCamelConventionFirstCharLower bool?

Whether to apply camel case convention (null to skip).

type Type

Type of the constants (e.g. string, int).

Returns

string

Generated constant declarations.

GetConsts(List<string>, bool?)

Generates static readonly string constants from the list with optional camel case convention.

public static string GetConsts(List<string> list, bool? toCamelConvention)

Parameters

list List<string>

List of values to generate constants from.

toCamelConvention bool?

Whether to apply camel case convention.

Returns

string

Generated constant declarations.

GetCtorInner(int, IList)

Generates constructor body assignments in the form "this.field = field;" for each value.

public static string GetCtorInner(int tabCount, IList values)

Parameters

tabCount int

Number of tabs for indentation.

values IList

List of field names to assign.

Returns

string

Formatted constructor inner body string.

GetDictionaryStringObject<Value>(int, List<string>, List<Value>, string, CSharpGeneratorArgs)

Generates C# code for creating and populating a Dictionary with string keys and typed values.

public static string GetDictionaryStringObject<Value>(int tabCount, List<string> keys, List<Value> values, string nameDictionary, CSharpGeneratorArgs argument)

Parameters

tabCount int

Number of tabs for indentation.

keys List<string>

List of string keys.

values List<Value>

List of values.

nameDictionary string

Variable name for the dictionary.

argument CSharpGeneratorArgs

Code generation arguments.

Returns

string

Generated C# code string.

Type Parameters

Value

Type of dictionary values.

GetDictionaryValuesFromDictionary(Dictionary<string, string>)

Generates C# dictionary initialization code from a string-to-string dictionary.

public static string GetDictionaryValuesFromDictionary(Dictionary<string, string> data)

Parameters

data Dictionary<string, string>

Source dictionary to generate code from.

Returns

string

Generated C# code string for dictionary initialization.

GetDictionaryValuesFromDictionary<Key, Value>(int, string, Dictionary<Key, Value>)

Generates C# code for dictionary value assignments from a source dictionary.

public static string GetDictionaryValuesFromDictionary<Key, Value>(int tabCount, string nameDictionary, Dictionary<Key, Value> dict) where Key : notnull

Parameters

tabCount int

Number of tabs for indentation.

nameDictionary string

Name of the dictionary variable in the generated code.

dict Dictionary<Key, Value>

Source dictionary to generate assignment code from.

Returns

string

Generated C# code for dictionary value assignments.

Type Parameters

Key

Type of dictionary keys.

Value

Type of dictionary values.

GetDictionaryValuesFromRandomValue<Key, Value>(int, string, List<Key>, Func<Value>)

Generates C# code for dictionary value assignments using random values from a factory function.

public static string GetDictionaryValuesFromRandomValue<Key, Value>(int tabCount, string nameDictionary, List<Key> keys, Func<Value> randomValue) where Key : notnull

Parameters

tabCount int

Number of tabs for indentation.

nameDictionary string

Variable name of the dictionary.

keys List<Key>

List of dictionary keys.

randomValue Func<Value>

Factory function that produces random values.

Returns

string

Generated C# code string.

Type Parameters

Key

Type of dictionary keys.

Value

Type of dictionary values.

GetDictionaryValuesFromTwoList(List<string>, List<string>)

Generates C# dictionary initialization code from two parallel lists of names and values.

public static string GetDictionaryValuesFromTwoList(List<string> names, List<string> chars)

Parameters

names List<string>

List of dictionary key names.

chars List<string>

List of dictionary values corresponding to each key.

Returns

string

Generated C# code string for dictionary initialization.

GetDictionaryValuesFromTwoList<Key, Value>(int, string, List<Key>, List<Value>, CSharpGeneratorArgs)

Generates C# code for populating a dictionary from two parallel lists of keys and values.

public static string GetDictionaryValuesFromTwoList<Key, Value>(int tabCount, string nameDictionary, List<Key> keys, List<Value> values, CSharpGeneratorArgs argument) where Key : notnull

Parameters

tabCount int

Number of tabs for indentation.

nameDictionary string

Name of the dictionary variable.

keys List<Key>

List of keys.

values List<Value>

List of values.

argument CSharpGeneratorArgs

Arguments controlling code generation behavior (e.g., splitKeyWith).

Returns

string

Generated C# code string.

Type Parameters

Key

Type of dictionary keys.

Value

Type of dictionary values.

GetInnerContentOfCodeElementClass(List<string>)

Extracts the inner content of a class code element by removing the outer braces.

public static string GetInnerContentOfCodeElementClass(List<string> lines)

Parameters

lines List<string>

Lines of source code containing the class definition.

Returns

string

The inner content between the first opening and last closing brace, or empty string if the code is empty or commented.

GetList(List<string>, string)

Generates a C# list declaration with the given name and values.

public static string GetList(List<string> input, string listName)

Parameters

input List<string>

List of string values to include.

listName string

Variable name for the generated list.

Returns

string

Generated C# list declaration code.

GetSummaryXmlDocumentation(List<string>)

Extracts the summary section from XML documentation comments in C# source code lines.

public static string GetSummaryXmlDocumentation(List<string> cs)

Parameters

cs List<string>

Lines of source code containing XML documentation comments.

Returns

string

The extracted summary text.

Imports(List<string>, bool)

Extracts all import statements from source code lines, optionally removing them.

public static CollectionWithoutDuplicatesDC<string> Imports(List<string> lines, bool remove = false)

Parameters

lines List<string>

Lines of source code.

remove bool

If true, removes the import lines from the source.

Returns

CollectionWithoutDuplicatesDC<string>

Collection of unique import statements found.

IsEmptyCommentedOrOnlyWithNamespace(string, List<string>, Action<List<string>>, List<string>)

Vrátí true i když obsahuje kód bez středníku (např. prázdnou třídu)

public static bool IsEmptyCommentedOrOnlyWithNamespace(string fnwoe, List<string> linesOriginal, Action<List<string>> RemoveBetweenIfAndEndif, List<string> csWithSharpIf)

Parameters

fnwoe string

File name without extension to check against csWithSharpIf list.

linesOriginal List<string>

Original source code lines to analyze.

RemoveBetweenIfAndEndif Action<List<string>>

Optional action to remove preprocessor directive blocks. Can be null.

csWithSharpIf List<string>

List of filenames that contain #if directives and should not be considered empty.

Returns

bool

True if the file contains only namespace declarations, usings, comments, or is empty.

IsEmptyOrCommented(List<string>)

Determines whether all lines are either empty or commented out.

public static bool IsEmptyOrCommented(List<string> lines)

Parameters

lines List<string>

Lines of source code to check.

Returns

bool

True if all non-empty lines start with a comment marker.

IsFieldVariableConst(string)

Determines whether a line contains a string field/variable/const declaration and extracts its name.

public static (bool, string?) IsFieldVariableConst(string line)

Parameters

line string

Source code line to analyze.

Returns

(bool, string)

Tuple of (isConstField, fieldName). fieldName is null if not a const field.

IsKeyword(string)

Determines whether the given string is a C# keyword. Call CsKeywords.Init before use.

public static bool IsKeyword(string con)

Parameters

con string

String to check against keyword lists.

Returns

bool

True if the string is a C# keyword.

LineWithClass(List<string>, bool)

Finds the line containing a class declaration. If mustDerive is true, only returns classes that inherit from another.

public static string? LineWithClass(List<string> lines, bool mustDerive)

Parameters

lines List<string>

Source code lines to search.

mustDerive bool

Whether the class must derive from another class.

Returns

string

The line containing the class declaration, or null if not found.

ParseFields(List<string>)

value - data type key - name

public static Dictionary<string, string> ParseFields(List<string> lines)

Parameters

lines List<string>

Lines containing field declarations to parse.

Returns

Dictionary<string, string>

Dictionary mapping field names (camelCase) to their data types.

RemoveBlockComments(string)

Removes block comments (/* ... */) and line comments (//) from source code using regex.

public static string RemoveBlockComments(string str)

Parameters

str string

Source code text to process.

Returns

string

Text with block and line comments removed.

RemoveComments(List<string>, bool, bool, bool)

Removes C# line comments and/or block comments from the given source code lines.

public static List<string> RemoveComments(List<string> listOrString, bool line = true, bool block = true, bool keepLinesNumbers = false)

Parameters

listOrString List<string>

Source code lines to process.

line bool

Whether to remove single-line comments.

block bool

Whether to remove block comments.

keepLinesNumbers bool

Whether to keep original line numbers (replace comments with empty lines instead of removing).

Returns

List<string>

Processed source code lines with comments removed.

RemoveComments(string, bool, bool, bool)

Removes line and/or block comments from source code text.

public static string RemoveComments(string listOrString, bool line = true, bool block = true, bool keepLinesNumbers = false)

Parameters

listOrString string

Source code text to process.

line bool

Whether to remove single-line comments.

block bool

Whether to remove block comments.

keepLinesNumbers bool

Whether to preserve line numbers by keeping newlines.

Returns

string

Source code with comments removed.

RemoveImportsUsings(List<string>, bool)

Removes import/using statements and leading empty lines from the source code lines. Edits the list directly.

public static void RemoveImportsUsings(List<string> l2, bool imports)

Parameters

l2 List<string>

Lines of source code to process.

imports bool

If true, removes import statements; otherwise removes using statements.

RemoveLineComments(List<string>)

Direct edit

public static List<string> RemoveLineComments(List<string> list)

Parameters

list List<string>

Returns

List<string>

RemoveNamespace(List<string>, CollectionWithoutDuplicatesDC<string>?, bool)

public static void RemoveNamespace(List<string> lines, CollectionWithoutDuplicatesDC<string>? removed, bool removeRegions = true)

Parameters

lines List<string>

Source code lines to modify in-place.

removed CollectionWithoutDuplicatesDC<string>

Collection to store removed namespace names (can be null).

removeRegions bool

Whether to also remove region directives.

RemoveNamespace(string)

Return written content to drive

public static Task<List<string>> RemoveNamespace(string item)

Parameters

item string

Returns

Task<List<string>>

RemoveRegions(List<string>)

Removes all #region and #endregion directives from the lines.

public static List<string> RemoveRegions(List<string> lines)

Parameters

lines List<string>

Source code lines to process.

Returns

List<string>

Lines with region directives removed.

RemoveUsing(string, CollectionWithoutDuplicatesDC<string>)

Removes specified using directives from the source text.

public static string RemoveUsing(string text, CollectionWithoutDuplicatesDC<string> removedUsings)

Parameters

text string

Source code text to process.

removedUsings CollectionWithoutDuplicatesDC<string>

Collection of namespace names to remove.

Returns

string

Text with the specified using directives removed.

RemoveXmlDocComments(List<string>)

Removes all XML documentation comments from the source code lines.

public static string RemoveXmlDocComments(List<string> list)

Parameters

list List<string>

Source code lines to process.

Returns

string

Cleaned source code with comments removed.

RemoveXmlDocCommentsExceptSummary(List<string>, ref bool)

Removes empty XML doc comment tags (like empty returns) except summary blocks.

public static string RemoveXmlDocCommentsExceptSummary(List<string> list, ref bool removedAnything)

Parameters

list List<string>

Source code lines to process.

removedAnything bool

Output flag indicating whether anything was removed.

Returns

string

Joined string of the modified lines.

ReplaceForConsts(string)

Replaces static readonly field declarations with const declarations in the given file.

public static Task ReplaceForConsts(string pathXlfKeys)

Parameters

pathXlfKeys string

Path to the file to process.

Returns

Task

SetValuesAsNamesToConsts(List<string>)

Converts static readonly string fields without values to const fields using their names as values.

public static void SetValuesAsNamesToConsts(List<string> text)

Parameters

text List<string>

Source code lines to modify in-place.

TrimEnd(List<string>, params char[])

Trims specified characters from the end of each string in the list.

public static List<string> TrimEnd(List<string> sf, params char[] toTrim)

Parameters

sf List<string>

List of strings to trim.

toTrim char[]

Characters to trim from the end.

Returns

List<string>

The modified list with trimmed strings.

Usings(List<string>, bool)

Extracts all using statements from source code lines, optionally removing them.

public static CollectionWithoutDuplicatesDC<string> Usings(List<string> lines, bool remove = false)

Parameters

lines List<string>

Lines of source code.

remove bool

If true, removes the using lines from the source.

Returns

CollectionWithoutDuplicatesDC<string>

Collection of unique using statements found.

Usings(List<string>, string, bool)

Extracts all statements matching the given keyword from source code lines, optionally removing them.

public static CollectionWithoutDuplicatesDC<string> Usings(List<string> lines, string keyword, bool remove = false)

Parameters

lines List<string>

Lines of source code.

keyword string

Keyword to match (e.g., "using" or "import").

remove bool

If true, removes matching lines from the source.

Returns

CollectionWithoutDuplicatesDC<string>

Collection of unique statements found.

Usings(List<string>, string, out List<int>, bool)

Extracts all statements matching the given keyword, outputting line indices and optionally removing them.

public static CollectionWithoutDuplicatesDC<string> Usings(List<string> lines, string keyword, out List<int> removeLines, bool remove = false)

Parameters

lines List<string>

Lines of source code.

keyword string

Keyword to match (e.g., "using" or "import").

removeLines List<int>

Output list of line indices where matching statements were found.

remove bool

If true, removes matching lines from the source.

Returns

CollectionWithoutDuplicatesDC<string>

Collection of unique statements found.

WrapWithQuote(Type, ref string)

Wraps the string value with quotes appropriate for the given type (double quotes for string, single for char).

public static void WrapWithQuote(Type tKey, ref string keyS)

Parameters

tKey Type

Type to determine the quote character.

keyS string

String value to wrap with quotes, modified in place.

WrapWithQuoteList(Type, IList)

Wraps each item in the list with appropriate quotes based on the value type and joins them with commas.

public static string WrapWithQuoteList(Type tValue, IList valueS)

Parameters

tValue Type

Type of values to determine quoting style.

valueS IList

List of values to wrap and join.

Returns

string

Comma-separated string of quoted values.

WrapWithRegion(string, string)

Wraps the given text in a C# region directive.

public static string WrapWithRegion(string text, string regionName)

Parameters

text string

Text content to wrap.

regionName string

Name for the region.

Returns

string

Text wrapped in region and endregion directives.