/CsToTs

Primary LanguageTypeScript

In a C# program, properties are used to define the characteristics of a class. A property typically consists of a pair of get and set accessor methods, which are used to read or set the value of a private field. Properties can be used to control data access and perform logical operations on the properties.

When you decide to share your project with the outside world through an API service, it can be helpful to assist the receiving developer by converting your C# property classes and enums to TypeScript property classes and enums. In this example project, we aim to convert our C# property classes and enum structures to TypeScript property classes and enums, specifically for projects prepared in TypeScript.

The main workflow of the program consists of the following steps:

Firstly, two constants are defined: BasePath and ExportFileName. BasePath represents the path to the folder where the DLL file is located, while ExportFileName represents the name of the generated TypeScript file.

The Main method is executed. This method subscribes to the CurrentDomain_AssemblyResolve event to load the DLL file. By doing so, when a specific DLL file is being loaded, the event is triggered, and the event handler loads the DLL file from the specified path.

The specified DLL file is loaded using the Assembly.Load method. The full path to the file is constructed using Path.Combine and File.ReadAllBytes to read the bytes of the DLL file.

Types are retrieved from the loaded DLL, and valid types are filtered using the IsValidType method. This filtering process checks the following conditions:

If the type is an interface, it is considered invalid and filtered out. If the type is an abstract type, it is considered invalid and filtered out. If the type's access modifier is not public, it is considered invalid and filtered out. If the type is an anonymous type, it is considered invalid and filtered out. If the type is a class, it is considered invalid and filtered out if its name does not end with "DTO" or "DA" or if it does not match a specific naming convention (e.g., "PersonDA"). You can modify this condition according to your own project's naming conventions. The CsToTs.Generator.GenerateTypeScript method is used to generate TypeScript code from the valid types. TypeRenamer and MemberRenamer options are specified for the generated TypeScript code. TypeRenamer and MemberRenamer are custom methods used to convert type and member names to CamelCase.

The generated TypeScript code is printed to the console using the Console.WriteLine method.

A file named ExportFileName is created, and the generated TypeScript code is written to it using the File.WriteAllText method.

I hope this explanation clarifies the process of converting C# property classes to TypeScript property interfaces in the given example program.