AdaskoTheBeAsT / Typewriter

Automatic TypeScript template generation from C# source files
http://frhagn.github.io/Typewriter
Apache License 2.0
66 stars 13 forks source link

If a property Type is enum, how to get the Enum object of it? #278 #24

Closed luizfbicalho closed 8 months ago

luizfbicalho commented 1 year ago

copied from https://github.com/frhagn/Typewriter/issues/278 I have a class with properties, and I want to generate a method for each property that the type is enum, and access the enum values. How can I convert a type to a Enum?

     IEnumerable<Enum> Enums(Class clas) 
{
 List<Enum> retorno = clas.Properties.Where(x=>x.Type.IsEnum).ToList(); 
return retorno; 
}
AdaskoTheBeAsT commented 9 months ago

https://github.com/AdaskoTheBeAsT/NetCoreTypewriterRecipes/blob/a01d6fe65272b7c51277d7b380baa3b673dd1fea/src/AngularWebApiSample2/ClientApp/apps/client-app/src/api/models/_AutogeneratedModels.tst#L400

luizfbicalho commented 9 months ago

I explained it the wrong way

I have a property that the type is Enum

List<Property> retorno = clas.Properties.Where(x=>x.Type.IsEnum).ToList();

And I want to get the Enum and the EnumValues from this property

something like

property.Type.ToEnum()

AdaskoTheBeAsT commented 8 months ago

enum value in property is only known in runtime - not possible to do this

for filtering properties of type enum you need to define helper method

in top of template (where helper methods are defined) bool PropertyIsEnum(Property property) { return property.Type.IsEnum; }

and use it like this $Properties($PropertyIsEnum)[ // generate for each enum property ]

luizfbicalho commented 8 months ago

I dont want the actual value. I want tô list the enum possible values from the property type

AdaskoTheBeAsT commented 8 months ago

you can't do this out of property - you can do this by annotating enum types as I already pasted - then you have fulll possibility to also grab all values

luizfbicalho commented 8 months ago

you can't do this out of property - you can do this by annotating enum types as I already pasted - then you have fulll possibility to also grab all values

that is my question, I needed to get the enum from the property type, if its not possible I'll try another way