c# - Manage string arguments passed to function without enum -
i'm using webrequest make calls api. want make wrapper call specifies variable routing use. @ moment, can come enum , switch statement.
public void webrequestwrapper(myenum arg) { string argument = string.empty; switch(arg) { case myenum.myspecifiedargument: argument = "myspecifiedargument"; break; // etc etc... } var request = new webrequest.create(myurl + argument); // etc, etc... }
my question is, there way around using enum don't have use switch statement? do:
public void webrequestwrapper(myargcollection arg) { var request = new webrequest.create(myurl + arg); // etc, etc... }
where myargcollection custom class/struct/something-like-an-enum , 'arg' represent string? know use reflection , enum helper classes, i've read that isn't particularly fast.
with enum, have:
enum myenum { stuff = 3, fred = 5, plant = 125 }
whereas i'd be:
enum myenum { stuff = "things in room", fred = "a guy down street", plant = "no longer alive" }
obviously, latter isn't legal. there way of using struct, class, whatever make work? please let me know if doesn't make sense. thanks!
update after question change: so, want attach custom string each enum value. can use attributes that:
enum myenum { [description("things in room")] stuff, [description("a guy down street")] fred, ... }
you'll need create descriptionattribute
class, of course.
then can add extension method:
public static string getdescription(this enum e);
this method see if enum's value has descriptionattribute
, , if - return content. if not, should return e.tostring()
.
answer old question: can use arg.tostring()
string representation.
Comments
Post a Comment