VOOZH about

URL: https://www.nuget.org/packages/CsCodeGenerator/

⇱ NuGet Gallery | CsCodeGenerator 2.0.3




👁 Image
CsCodeGenerator 2.0.3

dotnet add package CsCodeGenerator --version 2.0.3
 
 
NuGet\Install-Package CsCodeGenerator -Version 2.0.3
 
 
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CsCodeGenerator" Version="2.0.3" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CsCodeGenerator" Version="2.0.3" />
 
Directory.Packages.props
<PackageReference Include="CsCodeGenerator" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CsCodeGenerator --version 2.0.3
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CsCodeGenerator, 2.0.3"
 
 
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CsCodeGenerator@2.0.3
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CsCodeGenerator&version=2.0.3
 
Install as a Cake Addin
#tool nuget:?package=CsCodeGenerator&version=2.0.3
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

CodeGenerator

This is a small .NET library that enables easy C# code generation based on Classes and its elements.
It has ability to create ClassModels and write it to .cs files.
Can specify their Members (Constructor, Field, Property, Method) including Attributes and Parameters.
Defining namespace and using Directives is supported as well.
Library can also generate Enums and Interfaces, and create NestedClasses inside parent class.

BaseElement has Config for: IndentSize, Comment, CommentHasSummaryTag(df:true), AccessModifier, BuiltInDataType, CustomDataType, Name
Property more Config: IsGetOnly, IsAutoImplemented, GetterBody, SetterBody
CsGenerator Settings are: DefaultTabSize: 4 | OutputDirectory: "Output" |
-- List of components --
AccessModifier: public, private, protected, internal, protected_internal
BuiltInDataType: void, bool, byte, int, long, decimal, float, double, char, string, object
CommonDataType: DateTime, Guid
KeyWord: this, abstract, partial, static, new, virtual, override, sealed, const, async, readOnly
IndentType: None, Single, Double, Triple, Quadruple

For more complex code with indented segments specific Indent value should be set (prepend in a loop) for all internal lines/elements.

Package targets .NET Standard 2.0 so can be used both with .NetFramework and .NetCore / .Net (new unified)

Installation 👁 NuGet
package: 'Install-Package CsCodeGenerator'

Contributing

If you find this project useful you can mark it by leaving a Github *Star.

Please read for details on code of conduct, and the process for submitting pull requests.
👁 NuGet

Want to Contact us for Development & Consulting: www.codis.tech

Also take a look into others packages:</br> -Open source (MIT or cFOSS) authored .Net libraries (@Infopedia.io personal blog post) | № | .Net library | Description | | - | ------------------------ | -------------------------------------------------------- | | 1 | EFCore.BulkExtensions | EF Core Bulk CRUD Ops (Flagship Lib) | | 2 | EFCore.UtilExtensions | EF Core Custom Annotations and AuditInfo | | 3 | EFCore.FluentApiToAnnotation | Converting FluentApi configuration to Annotations | | 4 | FixedWidthParserWriter | Reading & Writing fixed-width/flat data files | | 5 | CsCodeGenerator | C# code generation based on Classes and elements | | 6 | CsCodeExample | Examples of C# code in form of a simple tutorial |

GeneratorModel Structure:

Class Inheritance Hierarchy

 |-* BaseElement
 ClassModel : -----|
Property : -- Field : ----------|
Constructor : Method : ---------|
 EnumModel : ------|
 InterfaceModel : -|

Component Composition

CsGenerator
|
|---Files
	|
	|---Enums
	|
	|---Classes
		|
		|---Fields
		|
		|---Constructors (DefaultConstructor first)
		|	|---Attributes
		|	|	|--- Parameters
		|	|
		|	|---Parameters
		|
		|---Properties
		|	|---Attributes
		|	|	|--- Parameters
		|	|
		|	|---Parameters
		|
		|---Methods
		|	|---Attributes
		|	|	|--- Parameters
		|	|
		|	|---Parameters
		|
		|---NestedClasses (recursively)
			|--- ...

How to use it

Following is first example of ComplexNumber class and then creating its ClassModel for writing to Complex.cs<br>

Class we want to generate:

using System;
using System.Model;

namespace CsCodeGenerator.Tests
{
 [Description("Some class info")]
 public partial class ComplexNumber : SomeBaseClass, NumbericInterface
 {
 protected const double PI = 3.14;
 private string remark;

 public ComplexNumber() { }

 public ComplexNumber(double real, double imaginary = 0)
 {
 Real = real;
 Imaginary = imaginary;
 }

 public static string DefaultFormat { get; } = "a + b * i";

 public double Real { get; set; }

 public double Imaginary { get; set; }

 public virtual string Remark
 {
 get { return remark; }
 set { remark = value; }
 }

 public double Modul()
 {
 return Math.Sqrt(Real * Real + Imaginary * Imaginary);
 }

 public ComplexNumber Add(ComplexNumber input)
 {
 ComplexNumber result = new ComplexNumber();
 result.Real = Real + input.Real;
 result.Imaginary = Imaginary + input.Imaginary;
 return result;
 }

 /// <summary>
 // example of 2 KeyWords(new and virtual), usually here would be just virtual
 /// <summary>
 public new virtual string ToString()
 {
 return $"({Real:0.00}, {Imaginary:0.00})";
 }
 }
}

Code to do it:

var usingDirectives = new List<string>
{
 "using System;",
 "using System.ComponentModel;"
};
string fileNameSpace = $"{Util.Namespace} CsCodeGenerator.Tests";
string complexNumberText = "ComplexNumber";

ClassModel complexNumberClass = new ClassModel(complexNumberText);
complexNumberClass.SingleKeyWord = KeyWord.Partial; // one way to set single KeyWord
//complexNumberClass.KeyWords.Add(KeyWord.Partial); // or alternative way
complexNumberClass.BaseClass = "SomeBaseClass";
complexNumberClass.Interfaces.Add("NumbericInterface)";

var descriptionAttribute = new AttributeModel("Description")
{
 SingleParameter = new Parameter(@"""Some class info""")
};
complexNumberClass.AddAttribute(descriptionAttribute);

complexNumberClass.DefaultConstructor.IsVisible = true;

Constructor secondConstructor = new Constructor(complexNumberClass.Name);
secondConstructor.Parameters.Add(new Parameter(BuiltInDataType.Double, "real"));
secondConstructor.Parameters.Add(new Parameter(BuiltInDataType.Double, "imaginary") { Value = "0" });
secondConstructor.BodyLines.Add("Real = real;");
secondConstructor.BodyLines.Add("Imaginary = imaginary;");
complexNumberClass.Constructors.Add(secondConstructor);

var fields = new Field[]
{
 new Field(BuiltInDataType.Double, "PI") { SingleKeyWord = KeyWord.Const, DefaultValue = "3.14" },
 new Field(BuiltInDataType.String, "remark") { AccessModifier = AccessModifier.Private },
}.ToDictionary(a => a.Name, a => a);

var properties = new Property[]
{
 new Property(BuiltInDataType.String, "DefaultFormat")
 {
 SingleKeyWord = KeyWord.Static,
 IsGetOnly = true,
 DefaultValue = @"""a + b * i"""
 },
 new Property(BuiltInDataType.Double, "Real"),
 new Property(BuiltInDataType.Double, "Imaginary"),
 new Property(BuiltInDataType.String, "Remark")
 {
 SingleKeyWord = KeyWord.Virtual,
 IsAutoImplemented = false,
 GetterBody = "remark",
 SetterBody = "remark = value"

 },
}.ToDictionary(a => a.Name, a => a);

var methods = new Method[]
{
 new Method(BuiltInDataType.Double, "Modul")
 {
 BodyLines = new List<string> { "return Math.Sqrt(Real * Real + Imaginary * Imaginary);" }
 },
 new Method(complexNumberText, "Add")
 {
 Parameters = new List<Parameter> { new Parameter("ComplexNumber", "input") },
 BodyLines = new List<string>
 {
 "ComplexNumber result = new ComplexNumber();",
 "result.Real = Real + input.Real;",
 "result.Imaginary = Imaginary + input.Imaginary;",
 "return result;"
 }
 },
 new Method(BuiltInDataType.String, "ToString")
 {
 Comment = "example of 2 KeyWords(new and virtual), usually here would be just virtual",
 KeyWords = new List<KeyWord> { KeyWord.New, KeyWord.Virtual },
 BodyLines = new List<string> { "return $\"({Real:0.00}, {Imaginary:0.00})\";" }
 }
}.ToDictionary(a => a.Name, a => a);

complexNumberClass.Fields = fields;
complexNumberClass.Properties = properties;
complexNumberClass.Methods = methods;

FileModel complexNumberFile = new FileModel(complexNumberText);
complexNumberFile.LoadUsingDirectives(usingDirectives);
complexNumberFile.Namespace = fileNameSpace;
complexNumberFile.Classes.Add(complexNumberClass.Name, complexNumberClass);

CsGenerator csGenerator = new CsGenerator();
csGenerator.Files.Add(complexNumberFile.Name, complexNumberFile);
csGenerator.CreateFiles(); //Console.Write(complexNumberFile); 
Product Versions Compatible and additional computed target framework versions.
.NET net5.0 net5.0 was computed.  net5.0-windows net5.0-windows was computed.  net6.0 net6.0 was computed.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 was computed.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 was computed.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
.NET Core netcoreapp2.0 netcoreapp2.0 was computed.  netcoreapp2.1 netcoreapp2.1 was computed.  netcoreapp2.2 netcoreapp2.2 was computed.  netcoreapp3.0 netcoreapp3.0 was computed.  netcoreapp3.1 netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 netstandard2.0 is compatible.  netstandard2.1 netstandard2.1 was computed. 
.NET Framework net461 net461 was computed.  net462 net462 was computed.  net463 net463 was computed.  net47 net47 was computed.  net471 net471 was computed.  net472 net472 was computed.  net48 net48 was computed.  net481 net481 was computed. 
MonoAndroid monoandroid monoandroid was computed. 
MonoMac monomac monomac was computed. 
MonoTouch monotouch monotouch was computed. 
Tizen tizen40 tizen40 was computed.  tizen60 tizen60 was computed. 
Xamarin.iOS xamarinios xamarinios was computed. 
Xamarin.Mac xamarinmac xamarinmac was computed. 
Xamarin.TVOS xamarintvos xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on CsCodeGenerator:

Package Downloads
KoloDev.GDS.QA.Accelerator

KoloDev Ltd. QA Accelerator. This package contains the solution accelerators for testing teams, enabling selenium, accessibility, cross browser testing and more.

KoloDev.GDS.QA

KoloDev Ltd. QA Accelerator. This package contains the solution accelerators for testing teams, enabling selenium, accessibility, cross browser testing and more.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.3 7,996 9/17/2024
2.0.2 293 9/10/2024
2.0.1 244 9/10/2024
2.0.0 92,871 10/30/2021
1.0.2 30,271 5/25/2017
1.0.1 3,427 3/2/2017
1.0.0 2,621 2/22/2017

Fluent calls feature