SharpCode

所属分类:自动编程
开发工具:C#
文件大小:73KB
下载次数:0
上传日期:2022-07-27 07:42:27
上 传 者sh-1993
说明:  小型C#代码生成器。以编程方式轻松生成代码!
(Small C# code generator. Easily generate code programmatically!)

文件列表:
.editorconfig (6337, 2022-07-27)
.version (5, 2022-07-27)
.vscode (0, 2022-07-27)
.vscode\settings.json (136, 2022-07-27)
CHANGELOG.md (3933, 2022-07-27)
SharpCode.ruleset (1994, 2022-07-27)
docs (0, 2022-07-27)
docs\errors.md (3987, 2022-07-27)
docs\releases.md (1257, 2022-07-27)
samples (0, 2022-07-27)
samples\JsonToNet (0, 2022-07-27)
samples\JsonToNet\JsonToNet.csproj (511, 2022-07-27)
samples\JsonToNet\Program.cs (10906, 2022-07-27)
samples\JsonToNet\Properties (0, 2022-07-27)
samples\JsonToNet\Properties\launchSettings.json (249, 2022-07-27)
samples\JsonToNet\input (0, 2022-07-27)
samples\JsonToNet\input\glossary.json (551, 2022-07-27)
samples\JsonToNet\input\types.json (335, 2022-07-27)
samples\JsonToNet\input\user.json (1352, 2022-07-27)
samples\JsonToNet\input\webapp.json (3718, 2022-07-27)
samples\Samples.sln (1700, 2022-07-27)
scripts (0, 2022-07-27)
scripts\CodeCoverage.ps1 (1257, 2022-07-27)
scripts\PackAndPublish.ps1 (2650, 2022-07-27)
src (0, 2022-07-27)
src\SharpCode.Test (0, 2022-07-27)
src\SharpCode.Test\.editorconfig (314, 2022-07-27)
src\SharpCode.Test\ClassBuilderTests.cs (22226, 2022-07-27)
src\SharpCode.Test\CodeTests.cs (13884, 2022-07-27)
src\SharpCode.Test\ConstructorBuilderTests.cs (10470, 2022-07-27)
src\SharpCode.Test\EnumBuilderTests.cs (10175, 2022-07-27)
... ...

# SharpCode Small C# code generator. Easily generate code programmatically! [![CI Status](https://img.shields.io/github/workflow/status/ful-stackz/SharpCode/CI?label=CI&logo=github&style=flat)](https://github.com/ful-stackz/SharpCode/actions?query=workflow%3ACI) [![Nuget](https://img.shields.io/nuget/v/SharpCode?color=success&label=nuget&logo=nuget&style=flat)](https://www.nuget.org/packages/SharpCode/) [![codecov](https://codecov.io/gh/ful-stackz/SharpCode/branch/main/graph/badge.svg?token=F2E4FV2DA3)](https://codecov.io/gh/ful-stackz/SharpCode) ## Install - .NET CLI `dotnet add package SharpCode` - Package Manager `Install-Package SharpCode` - Package Reference `` ## Usage
Simple usage ```csharp using SharpCode; var sourceCode = Code.CreateNamespace("Data") .WithClass(Code.CreateClass("User") .WithProperty(Code.CreateProperty("int", "Id")) .WithProperty(Code.CreateProperty("string", "Username"))) .ToSourceCode(); System.IO.File.WriteAllText("User.cs", sourceCode); // User.cs namespace Data { public class User { public int Id { get; set; } public string Username { get; set; } } } ```
Extended usage ```csharp using SharpCode; var dataNamespace = Code.CreateNamespace("Data"); var userDetailsClass = Code.CreateClass("UserDetails", AccessModifier.Public) .WithField(Code.CreateField("int", "_id", AccessModifier.Private).MakeReadonly()) .WithField(Code.CreateField("string", "_username", AccessModifier.Private).MakeReadonly()) .WithConstructor(Code.CreateConstructor() .WithAccessModifier(AccessModifier.Public) .WithParameter("int", "id", "_id") .WithParameter("string", "username", "_username")) .WithProperty(Code.CreateProperty("int", "Id", AccessModifier.Public) .WithGetter("_id") .WithoutSetter()) .WithProperty(Code.CreateProperty("string", "Username", AccessModifier.Public) .WithGetter("_username") .WithoutSetter()); var userClass = Code.CreateClass("User", AccessModifier.Public) .WithProperty(Code.CreateProperty("UserDetails", "Details", AccessModifier.Public) .WithoutSetter()) .WithConstructor(Code.CreateConstructor() .WithAccessModifier(AccessModifier.Public) .WithParameter("UserDetails", "details", "Details")); System.IO.File.WriteAllText( "User.cs", dataNamespace .WithClass(userDetailsClass) .WithClass(userClass) .ToSourceCode()); // User.cs namespace Data { public class UserDetails { private readonly int _id; private readonly string _username; public UserDetails(int id, string username) { _id = id; _username = username; } public int Id { get => _id; } public string Username { get => _username; } } public class User { public User(UserDetails details) { Details = details; } public UserDetails Details { get; } } } ```
## Samples The [samples](https://github.com/ful-stackz/SharpCode/tree/main/samples) folder contains sample projects which make use of `SharpCode`. The projects are fully functional and can be played around with. Check the `samples/README.md` for more information. ## Features With `SharpCode` you can programmatically generate source code for a lot of C# structures. When generating the source code of any structure `SharpCode` performs basic validation to ensure a level of correctness of the produced source code.
Generating namespace source code ```csharp // Define the name of the namespace namespace Data { // Interface members with different access modifiers public interface IHasData {} // Class members with different access modifiers public class Data {} // Struct members with different access modifiers public struct DataPoint {} // Enum members with different access modifiers public enum DataType {} } ```
Generating interface source code ```csharp // Define XML summary docs // Define the name of the interface // Define a list of implemented interfaces /// /// Docs! /// public interface IHasData : IImplementedInterface { // Define property members int Count { get; set; } } ```
Generating class source code ```csharp // Define XML summary docs // Define the name of the class // Define an inherited class // Define a list of implemented interfaces /// /// Docs! /// public class Data : DataBase, IHasData { // Define field members private int _count; // Define constructors // Define constructor XML summary docs // Define constructor parameters // Define accepting fields for constructor parameters // Define base calls with parameters /// /// Docs! /// public Data(int count) : DataBase(count) { _count = count; } // Define property members // Define property XML summary docs // Define custom getter/setter for properties /// /// Docs! /// public int Count { get => _count; set => _count = value; } // Define auto-implemented properties public string Name { get; set; } } ```
Generating struct source code ```csharp // Define XML summary docs // Define the name of the struct // Define a list of implemented interfaces /// /// Docs! /// public struct DataPoint : IHasData { // Define field members private int _count; // Define constructors // Define constructor XML summary docs // Define constructor parameters // Define accepting fields for constructor parameters /// /// Docs! /// public Data(int count) { _count = count; } // Define property members // Define property XML summary docs // Define custom getter/setter for properties /// /// Docs! /// public int Count { get => _count; set => _count = value; } } ```
Generating enum source code ```csharp // Define XML summary docs // Define the name of the enum /// /// Docs! /// public enum DataType { // Define enum members // Define XML summary docs for enum members // Define explicit values for enum members /// /// Docs! /// Invalid = 0, Incomplete, Complete, } ``` ```csharp // Define enum as flags // Auto generated flags-compatible values for enum members [System.Flags] public enum ExampleFlags { None = 0, A = 1, B = 2, C = 4, D = 8, } ```
## Development You don't need anything special for local development. - `dotnet build ./src/SharpCode` - will build you your own local `SharpCode` - `dotnet add reference ` - will add `SharpCode` as a local reference to your project - `dotnet test ./src/SharpCode.Test` - will run the tests - `dotnet watch test ./src/SharpCode.Test` - will run the tests everytime you make a change to the source code of `SharpCode` itself or the tests ## Contributing This library is still in its early stages and being figured out. Many changes are expected and incoming. For your own sake please don't work on any big changes as the files might disappear before you're ready. Nonetheless, insights into the usage of the library, structure and general suggestions are always welcome! Thank you!

近期下载者

相关文件


收藏者