|
1 | | -# CSharpToJavaScript |
| 1 | +# CSharpToJavaScript |
| 2 | +Brute forcing conversion(translation) from C# to Javascript. |
| 3 | + |
| 4 | +This is a personal project with purpose to learn and understand better c# and js at the same time. Many stuff is not supported and some won't. Updates will be happening when I'm using this library.(irregular) |
| 5 | + |
| 6 | +| C# | Javascript | |
| 7 | +| --- | --- | |
| 8 | +| <pre>using static CSharpToJavaScript.APIs.JS.GlobalThis;<br>namespace ConsoleApp1twest.CSharp;<br><br>public class Test<br>{<br> public Test()<br> {<br> Console.Log("HelloWorld!");<br> }<br>}</pre> | <pre>class Test<br>{<br> constructor()<br> {<br> console.log("HelloWorld!");<br> }<br>}</pre>| |
| 9 | + |
| 10 | +## How to use |
| 11 | +*Nuget package will be available once I figure out how to do docs for api(should be easy) and what to do with esmascript and how to convert to c#.* |
| 12 | + |
| 13 | +- Download a library(Code-Local-Download ZIP) |
| 14 | +- Create c# project or use existed one |
| 15 | +- Follow [this](https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio?pivots=dotnet-7-0#add-a-project-reference) to add reference to project |
| 16 | +- In the Main method add: |
| 17 | +```csharp |
| 18 | +Assembly assembly = Assembly.GetExecutingAssembly(); |
| 19 | +CSTOJS cstojs = new(assembly); |
| 20 | +cstojs.Generate2Async("FULL PATH TO CSHARP FILE YOU WHAT TO CONVERT"); |
| 21 | +``` |
| 22 | +- Run program and file will be generated in output path(default is "Directory.GetCurrentDirectory()") with name "main.js"(default) |
| 23 | +- See below for simple example "HelloWorld" |
| 24 | + |
| 25 | +## Example "HelloWorld" |
| 26 | +Program.cs |
| 27 | +```csharp |
| 28 | +using System.Reflection; |
| 29 | +using CSharpToJavaScript; |
| 30 | +namespace ProgramTest; |
| 31 | + |
| 32 | +public class Program |
| 33 | +{ |
| 34 | + public static void Main() |
| 35 | + { |
| 36 | + Assembly executingAssembly = Assembly.GetExecutingAssembly(); |
| 37 | + |
| 38 | + CSTOJSOptions opt = new() |
| 39 | + { |
| 40 | + CustomCSNamesToJS = new List<Tuple<string, string>>() |
| 41 | + { |
| 42 | + new Tuple<string, string>("Console","console"), |
| 43 | + new Tuple<string, string>("WriteLine", "log") |
| 44 | + }, |
| 45 | + OutPutFileName = "test.js" |
| 46 | + }; |
| 47 | + |
| 48 | + CSTOJS cstojs = new(executingAssembly, opt); |
| 49 | + cstojs.Generate2Async("C:\\GitReps\\Program\\Program\\CSharp\\test.cs"); |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | +test.cs |
| 54 | +```csharp |
| 55 | +namespace ProgramTest; |
| 56 | + |
| 57 | +public class Test |
| 58 | +{ |
| 59 | + public Test() |
| 60 | + { |
| 61 | + Console.WriteLine("HelloWorld"); |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | +Above code will generate test.js file that contains: |
| 66 | +```javascript |
| 67 | +class Test |
| 68 | +{ |
| 69 | + constructor() |
| 70 | + { |
| 71 | + console.log("HelloWorld"); |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | +## Some Todos |
| 76 | +- [ ] More comments in code, especially in [CSTOJSOptions](https://github.com/TiLied/CSharpToJavaScript/blob/master/CSharpToJavaScript/CSTOJSOptions.cs) |
| 77 | +- [ ] Wiki? |
| 78 | +- [ ] Better and more examples |
| 79 | +- [ ] Figure out how to do docs for [api](https://github.com/TiLied/CSharpToJavaScript/tree/master/CSharpToJavaScript/APIs/JS) |
| 80 | + |
| 81 | +## Related Repository |
| 82 | +https://github.com/TiLied/GenDocsLib |
| 83 | + |
| 84 | +https://github.com/TiLied/GenCSharpLib |
| 85 | + |
| 86 | +## Thanks and usings |
| 87 | +[Microsoft CodeAnalysis CSharp](https://www.nuget.org/packages/Microsoft.CodeAnalysis.CSharp/) nuget package |
| 88 | + |
| 89 | +[MDN-content](https://github.com/mdn/content) for js docs |
| 90 | + |
| 91 | + |
0 commit comments