c# - Restrict Enum parameter to certain Enum types (at compile-time) -
i created static void method (enum myenum)
, i'd feed enum types, or face mistake @ compile-time. i'm experimenting lot code (using gameplay mechanics shader variants), need change accepted types on whim, least possible maintenance of method
class.
basically, i'm struggling enums have done working classes (create shared base class use parameter type). first came mind attributes:
- can custom attribute affect compilation in way? i'd surprised if could.
- can mimic class inheritance in any way enum types, albeit create kind of category restrict method parameters?
this impossible dream code:
[attributeusage(attributetargets.enum)] public class shadervariant : system.attribute { } void method (t myenum) t : [shadervariant]enum { }
my current options are:
- create aforementioned attribute tag selected enum declarations, , react during runtime (throw argumentexception if myenum.gettype() doesn't have attribute). approach silently allows me mess while coding, having add/remove attribute enum want dream-workflow.
- declare ludicrous amount of wrapper overloads each type , forever maintain
method
class, doomed commit same file on , on again every slight change made project. works @ compile-time.
this question how c# works. know it's easier create wrapper overloads, feels wrong subjugating handler method support parameter property.
enums short names constant numbers. if members of enum different, has acceptable in compile time method , not.. maybe need 2 enums. 1 have members acceptable method 1 having possible options.
enums simple structures without inheritance. if see feel need inheritance, should not use enum , use classes.
Comments
Post a Comment