This project is intended to create an easy to use CodeGenerator for C#.
Currently, this is only targeting C# to TypeScript
as the development use case.
You can find it in NuGet. The package name is DotnetCodegen.
Or simply install it using the dotnet cli
dotnet add package DotnetCodegen
Simply annotate the C# classes that you would like to generate their TypeScript interfaces
.
[GenerateTs]
public class YourClass
{
public int SomeProp { get; set; }
}
Create a generator instance and invoke its Generate()
method. You can configure the generator via its Option
param.
var generator = new TypeScript(new Option()
{
RelativeBaseOutputPath = "./Outputs",
TargetAssemblies = new()
{
Assembly.GetAssembly(typeof(Dummy)),
}
});
generator.Generate();
By default if child type is not annotated, the corresponding output TS type will be any
.
If you would like the child type to be generated as well, just mark the child type with GenerateTs
attribute as well.
[GenerateTs]
public class YourClass
{
public List<YourChildClass> SomeProp { get; set; }
}
[GenerateTs]
public class YourChildClass
{
public int SomeProp { get; set; }
}
All classes annotated with GenerateTs
will get their TS interfaces generated.
The are some parameters in the attribute to customize the code generation behavior too. For full list, refer to https://github.com/DriLLFreAK100/dotnet-codegen/blob/main/CodeGenerator/Attributes/GenerateTsAttribute.cs
Basically,
Path
- path to generate the file (within [Absolute|Relative]BaseOutputPath configured in generatorOption
)FileName
- custom name for the generated file
You can also perform a dry run to check what are the outputs from the generation by specifying IsDryRun
as true
in the option
var generator = new TypeScript(new Option()
{
IsDryRun = true,
RelativeBaseOutputPath = "./Outputs",
TargetAssemblies = new()
{
Assembly.GetAssembly(typeof(Dummy)),
}
});
generator.Generate();
It is totally up to your creativity. Just create the generator and invoke its Generate()
method.
For ASP.NET Web API, a good place to do this might be your Configure()
method at the app startup
. That way, you can generate the interfaces everytime you make changes and restart your dev server. However, remember to include condition to generate only when it is dev
environment. This is to prevent unnecessary code generations in production
environment.
For this to work like an on-demand code generation tool, you can also import it into a Console app
and run it on-demand.
Really, it's just about configuring the generator and call the Generate()
method as you deem fit.
For the full list of options, refer to Option.cs
at https://github.com/DriLLFreAK100/cs-codegen/blob/main/CodeGenerator/Models/Option.cs
Basically,
IsDryRun
- whether to generate physical files or only metadata for checking purposeAbsoluteBaseOutputPath
- absolute path to generate physical files. It takes precedence overRelativeBaseOutputPath
RelativeBaseOutputPath
- path to generate physical files, relative from the program execution pathLineSeparator
- next line (default to follow environment)TargetAssemblies
- target assemblies to scan for code generation. All classes marked byGenerateTs
within the assemblies will be generated
Any thoughts and inputs are always welcomed!