using System.Collections.Generic;
[System.AttributeUsage(System.AttributeTargets.Class |
System.AttributeTargets.Struct,
public class JavaScriptClass : System.Attribute
public string javascript_name;
[System.AttributeUsage(System.AttributeTargets.All,
public class JavaScriptProperty : System.Attribute
public string javascript_name;
public string javascript_type;
public string handler_prefix;
[JavaScriptClass(javascript_name = "Details", api_url="`${this.baseUrl}/api/details/${id}`", api_args = "id: number")]
[JavaScriptProperty(javascript_name = "date", javascript_type = "Date", handler_prefix = "date_")]
[JavaScriptProperty(javascript_name = "score", javascript_type = "number")]
[JavaScriptProperty(javascript_name = "description", javascript_type = "string")]
public string description;
[JavaScriptProperty(javascript_name = "approved", javascript_type = "boolean")]
public class JavaScriptConverter {
public string javascript_name;
public string javascript_type;
public string handler_prefix;
public string javascript_name;
public Dictionary<string, Field> fields;
public JavaScriptConverter(string class_name) {
fields = new Dictionary<string, Field>();
private static JavaScriptConverter to_js_handler<T>() where T : class, new() {
T class_instance = new T();
System.Type class_type = class_instance.GetType();
JavaScriptConverter converter = new JavaScriptConverter(class_type.Name);
System.Attribute[] attrs = System.Attribute.GetCustomAttributes(class_type);
foreach (System.Attribute attr in attrs)
if (attr is JavaScriptClass)
JavaScriptClass a = (JavaScriptClass)attr;
converter.javascript_name = a.javascript_name;
converter.api_url = a.api_url;
converter.api_args = a.api_args;
foreach(System.Reflection.FieldInfo field_info in class_type.GetFields())
RuntimeFieldHandle field_handle = field_info.FieldHandle;
FieldInfo field = FieldInfo.GetFieldFromHandle(field_handle);
foreach (System.Attribute field_attr in System.Attribute.GetCustomAttributes(field))
if (field_attr is JavaScriptProperty)
JavaScriptConverter.Field converter_field = new JavaScriptConverter.Field();
JavaScriptProperty f = (JavaScriptProperty)field_attr;
converter_field.name = field.Name;
converter_field.javascript_name = f.javascript_name;
converter_field.javascript_type = f.javascript_type;
if (f.handler_prefix == null) {
converter_field.handler_prefix = "";
converter_field.handler_prefix = f.handler_prefix;
converter.fields[field.Name] = converter_field;
private static void dump_handler(JavaScriptConverter converter) {
System.Console.WriteLine("export class {0} {{", converter.javascript_name);
foreach (JavaScriptConverter.Field field in converter.fields.Values) {
System.Console.WriteLine("\t{0}: {1};", field.javascript_name, field.javascript_type);
System.Console.WriteLine("\n\tconstructor(data: any) {\n\t\tObject.assign(this, data);\n\t}");
System.Console.WriteLine("}\n");
System.Console.WriteLine("public get{0}({1}): Promise<{0}> {{", converter.javascript_name, converter.api_args);
System.Console.WriteLine("\treturn this.http");
System.Console.WriteLine("\t\t.get<{0}>({1})", converter.javascript_name, converter.api_url);
System.Console.WriteLine("\t\t\t.map(response => new {0}(response.json()))", converter.javascript_name);
System.Console.WriteLine("\t\t\t.toPromise();\n})");
public static void Main()