У меня есть класс C#:
public class PhaseEnum : SmartEnum<PhaseEnum>
{
public static PhaseEnum AP = new PhaseEnum(1, "A. P Phase", "Ap", "A: Design Publish phase");
public static PhaseEnum AC = new PhaseEnum(2, "A. C Phase", "Ac", "A: Design Convert phase");
public static PhaseEnum B = new PhaseEnum(3, "B. Build Phase", "B", "B: Design Build phase");
public static PhaseEnum C = new PhaseEnum(4, "C. Run Phase", "C", "C: Design Run phase");
public static PhaseEnum D = new PhaseEnum(5, "D. Deploy Phase", "D", "D: Design Deploy Phase");
public readonly string Name;
public readonly string Code;
public PhaseEnum(int id, string name, string code, string value) : base(value, id)
{
Name = name;
Code = code;
}
}
Я конвертирую этот класс в код TS с помощью скрипта TypeWriter:
${
using Typewriter.Extensions.Types;
using Typewriter.Extensions.WebApi;
using System.Reflection;
Template(Settings settings)
{
settings.IncludeProject("Test.Work.Common");
settings.IncludeProject("Test.Dummy.Models");
settings.PartialRenderingMode = PartialRenderingMode.Combined;
settings.OutputExtension = ".ts";
}
string Inherit(Class c)
{
if (c.BaseClass!=null)
return "extends " + c.BaseClass.ToString();
else
return "";
}
string Imports(Class c){
return (c.BaseClass!=null) ? ( "import {" + c.BaseClass.ToString() +"} from \"./" + c.BaseClass.ToString() +"\";") :"";
}
string Constructor(Class c){
List<string> arguments;
arguments = c.Properties.Select(f => f.name + ": " + f.Type).ToList();
arguments.AddRange(c.Fields.Select(f => f.name + ": " + f.Type));
if(c.BaseClass!=null){
arguments.AddRange(c.BaseClass.Properties.Select(p => p.name + ": " + p.Type));
}
return string.Join(", ", arguments);
}
string Super(Class c){
if(c.BaseClass!=null){
List<string> baseProperties = c.BaseClass.Properties.Select(p => p.name).ToList();
return "super(" + string.Join(", ",baseProperties) + ");";
}
return "";
}
string FieldValues(Class c){
FieldInfo[] fields = c.GetType().GetFields( BindingFlags.Public | BindingFlags.Static );
Console.WriteLine(fields);
var returnString = "";
foreach(var field in fields){
returnString = returnString + "public " + field.Name + ": " + c.Name + " = new " + c.Name + "(" + field.GetValue(null).ToString() + ");";
}
return returnString;
}
}
$Classes(*Enum)[
$Imports
export class $Name $Inherit {
constructor($Constructor) {
$Super
$Properties[ this.$Name = $name; ]
$Fields[ this.$Name = $name; ]
}
$Properties[
public $Name: $Type;
]
$Fields[
public $Name: $Type;
]
$FieldValues
}
]
Я попытался использовать здесь Reflection, чтобы получить publi c stati c и их значения в функции FieldValues, но они не отображаются. В результате я получаю:
import {SmartEnum} from "./SmartEnum";
export class PhaseEnum extends SmartEnum {
constructor(name: string, code: string, value: string, id: number) {
super(value, id);
this.Name = name; this.Code = code;
}
public Name: string;
public Code: string;
}
Итак, есть ли способ получить поля publi c stati c вместе с их значениями?