Table of Contents

Model example


For example this model:

public partial class User
{
	private int _ID = 0;
	public string FirstName { get; set; } = string.Empty;
	public string LastName { get; set; } = string.Empty;
}

Will be translated as:

class User
{
	_ID = 0;
	#_FirstName_= String.raw``;
	get FirstName(){return this.#_FirstName_;}
	set FirstName(value){this.#_FirstName_ = value;}
	#_LastName_= String.raw``;
	get LastName(){return this.#_LastName_;}
	set LastName(value){this.#_LastName_ = value;}
}

The library provides various tools to improve or change the output.

For example, if you want to ignore "_ID", you can use IgnoreAttribute:

using CSharpToJavaScript.Utils;
public partial class User
{
	[Ignore]
	private int _ID = 0;
	public string FirstName { get; set; } = string.Empty;
	public string LastName { get; set; } = string.Empty;
}
Important

A C# project must have a CSharpToJavaScript reference, you can add it through NuGet.

class User
{
	#_FirstName_= String.raw``;
	get FirstName(){return this.#_FirstName_;}
	set FirstName(value){this.#_FirstName_ = value;}
	#_LastName_= String.raw``;
	get LastName(){return this.#_LastName_;}
	set LastName(value){this.#_LastName_ = value;}
}

With IgnoreAttribute you can ignore fields, properties, methods, or even classes!

With CSTOJSOptions you can change formating. For example, with "KeepBraceOnTheSameLine":

using CSharpToJavaScript.Utils;
public partial class User
{
	[Ignore]
	private int _ID = 0;
	public string FirstName { get; set; } = string.Empty;
	public string LastName { get; set; } = string.Empty;
}
class User {
	#_FirstName_= String.raw``;
	get FirstName(){return this.#_FirstName_;}
	set FirstName(value){this.#_FirstName_ = value;}
	#_LastName_= String.raw``;
	get LastName(){return this.#_LastName_;}
	set LastName(value){this.#_LastName_ = value;}
}

Another useful option is "MakePropertiesEnumerable", properties in JS are NOT enumerable and JSON will ignore them. This option defines properties with "Object.defineProperty".

Important

Class must have constructor!

using CSharpToJavaScript.Utils;
public partial class User
{
	[Ignore]
	private int _ID = 0;
	public string FirstName { get; set; } = string.Empty;
	public string LastName { get; set; } = string.Empty;
	
	public User(){}
}
class User {
	#_FirstName_= String.raw``;
	#_LastName_= String.raw``;
	
	constructor(){Object.defineProperty(this, 'LastName', { enumerable: true, get: function() { return this.#_LastName_; }, set: function (value) { this.#_LastName_ = value; } });
Object.defineProperty(this, 'FirstName', { enumerable: true, get: function() { return this.#_FirstName_; }, set: function (value) { this.#_FirstName_ = value; } });
}
}

Formatting is not that great... You can use "NormalizeWhitespace" to improve it.

class User {
    #_FirstName_ = String.raw``;
    #_LastName_ = String.raw``;
   constructor() {
       Object.defineProperty(this, 'LastName', { enumerable: true, get: function() { return this.#_LastName_; }, set: function (value) { this.#_LastName_ = value; } });
       Object.defineProperty(this, 'FirstName', { enumerable: true, get: function() { return this.#_FirstName_; }, set: function (value) { this.#_FirstName_ = value; } });
   }
}