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
Using
C# using keyword prefix.
public const string Using = "using "
Field Value
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
BasePathsHelperVsstringBase path to the Visual Studio projects folder.
Returns
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
dictDictionary<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
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
typestringC# 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
typestringSQLite 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
tabCountintNumber of tabs for indentation.
nameDictionarystringName for the generated class and dictionary.
dataDictionary<Key, Value>Source dictionary to generate code from.
argumentCSharpGeneratorArgsOptional code generation arguments.
Returns
- string
Generated C# code for the class with dictionary initialization.
Type Parameters
KeyType of dictionary keys.
ValueType 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
tabCountintNumber of tabs for indentation.
nameDictionarystringName for the generated class and dictionary.
keysList<Key>List of keys for the dictionary.
randomValueFunc<Value>Function that generates a random value for each entry.
argumentCSharpGeneratorArgsOptional code generation arguments.
Returns
- string
Generated C# code for the class with dictionary initialization.
Type Parameters
KeyType of dictionary keys.
ValueType 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
Returns
GenerateProperties(GeneratePropertiesArgs)
Generates C# property declarations with backing fields from the given arguments.
public static string GenerateProperties(GeneratePropertiesArgs argument)
Parameters
argumentGeneratePropertiesArgsArguments 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
Returns
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
namesList<string>Optional list of custom constant names (can be null to use values as names).
listList<string>List of values for the constants.
toCamelConventionFirstCharLowerbool?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
namesList<string>Optional list of custom constant names (can be null to use values as names).
listList<string>List of values for the constants.
toCamelConventionFirstCharLowerbool?Whether to apply camel case convention (null to skip).
typeTypeType 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
listList<string>List of values to generate constants from.
toCamelConventionbool?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
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
tabCountintNumber of tabs for indentation.
keysList<string>List of string keys.
valuesList<Value>List of values.
nameDictionarystringVariable name for the dictionary.
argumentCSharpGeneratorArgsCode generation arguments.
Returns
- string
Generated C# code string.
Type Parameters
ValueType 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
dataDictionary<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
tabCountintNumber of tabs for indentation.
nameDictionarystringName of the dictionary variable in the generated code.
dictDictionary<Key, Value>Source dictionary to generate assignment code from.
Returns
- string
Generated C# code for dictionary value assignments.
Type Parameters
KeyType of dictionary keys.
ValueType 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
tabCountintNumber of tabs for indentation.
nameDictionarystringVariable name of the dictionary.
keysList<Key>List of dictionary keys.
randomValueFunc<Value>Factory function that produces random values.
Returns
- string
Generated C# code string.
Type Parameters
KeyType of dictionary keys.
ValueType 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
namesList<string>List of dictionary key names.
charsList<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
tabCountintNumber of tabs for indentation.
nameDictionarystringName of the dictionary variable.
keysList<Key>List of keys.
valuesList<Value>List of values.
argumentCSharpGeneratorArgsArguments controlling code generation behavior (e.g., splitKeyWith).
Returns
- string
Generated C# code string.
Type Parameters
KeyType of dictionary keys.
ValueType 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
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
inputList<string>List of string values to include.
listNamestringVariable 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
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
linesList<string>Lines of source code.
removeboolIf 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
fnwoestringFile name without extension to check against csWithSharpIf list.
linesOriginalList<string>Original source code lines to analyze.
RemoveBetweenIfAndEndifAction<List<string>>Optional action to remove preprocessor directive blocks. Can be null.
csWithSharpIfList<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
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
linestringSource code line to analyze.
Returns
IsKeyword(string)
Determines whether the given string is a C# keyword. Call CsKeywords.Init before use.
public static bool IsKeyword(string con)
Parameters
constringString 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
linesList<string>Source code lines to search.
mustDeriveboolWhether 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
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
strstringSource 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
listOrStringList<string>Source code lines to process.
lineboolWhether to remove single-line comments.
blockboolWhether to remove block comments.
keepLinesNumbersboolWhether to keep original line numbers (replace comments with empty lines instead of removing).
Returns
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
listOrStringstringSource code text to process.
lineboolWhether to remove single-line comments.
blockboolWhether to remove block comments.
keepLinesNumbersboolWhether 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
l2List<string>Lines of source code to process.
importsboolIf true, removes import statements; otherwise removes using statements.
RemoveLineComments(List<string>)
Direct edit
public static List<string> RemoveLineComments(List<string> list)
Parameters
Returns
RemoveNamespace(List<string>, CollectionWithoutDuplicatesDC<string>?, bool)
public static void RemoveNamespace(List<string> lines, CollectionWithoutDuplicatesDC<string>? removed, bool removeRegions = true)
Parameters
linesList<string>Source code lines to modify in-place.
removedCollectionWithoutDuplicatesDC<string>Collection to store removed namespace names (can be null).
removeRegionsboolWhether to also remove region directives.
RemoveNamespace(string)
Return written content to drive
public static Task<List<string>> RemoveNamespace(string item)
Parameters
itemstring
Returns
RemoveRegions(List<string>)
Removes all #region and #endregion directives from the lines.
public static List<string> RemoveRegions(List<string> lines)
Parameters
Returns
RemoveUsing(string, CollectionWithoutDuplicatesDC<string>)
Removes specified using directives from the source text.
public static string RemoveUsing(string text, CollectionWithoutDuplicatesDC<string> removedUsings)
Parameters
textstringSource code text to process.
removedUsingsCollectionWithoutDuplicatesDC<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
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
listList<string>Source code lines to process.
removedAnythingboolOutput 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
pathXlfKeysstringPath to the file to process.
Returns
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
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
Returns
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
linesList<string>Lines of source code.
removeboolIf 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
linesList<string>Lines of source code.
keywordstringKeyword to match (e.g., "using" or "import").
removeboolIf 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
linesList<string>Lines of source code.
keywordstringKeyword to match (e.g., "using" or "import").
removeLinesList<int>Output list of line indices where matching statements were found.
removeboolIf 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
tKeyTypeType to determine the quote character.
keySstringString 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
tValueTypeType of values to determine quoting style.
valueSIListList 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
Returns
- string
Text wrapped in region and endregion directives.