c# - Multi Mapping Enum at runtime -


due question here marked duplicated, here repeat question , make more explanation question. hope can give suggestions. =======================================

as described in question, here enum:

public enum myenum {  one,  two,  three} 

is going parsed based on mutl strings.for example, below strings going parsed myenum.two:

"two", "two", "second", "2" 

the reason can't leverage description attributes or mapping dictionary directly mapping strings come external xml file. mapping change after period of time , it's not possible adding description attribute above declared enum.

for example, mappings above build 1 month ago , there items required below , these new items add xml file:

"2nd", "the second one" 

furthermore, use self developed script engine parse above , other similar needs (that's why don't use simple mapping function) :

object x = engine.execute( /*some script codes defined in external xml*/ ); if (returntype.isenum && enum.isdefined(returntype, x)) //returntype defined in external xml file     return enum.parse(returntype, x.tostring()); else if (returntype.isenum) {     // hope can handle mapping here } else     return convert.changetype(x, returntype); 

it seems strange pass-in mapping table above codes because designed generic script executing purpose. think @xanatos (in prevous 'duplicated' question) gave way resolve issue, mapping (via customized attributes) should generated @ runtime. attributes seems meta data , can't changed after compilation, however, found posts can use typedescriptor add attributes @ runtime, possible? , how?

the fact data comes external file contents change, makes etl/integration problem. in such cases have put cleanup/normalization step before parsing data.

in such cases, typical solution create lookup tables map inputs recognized outputs , replace input lookup values before parsing. in fact, etl tools sql server's integration services include lookup transformations purpose.

once replace incoming data lookup data, can parse using
enum.tryparse (string, boolean) or enum.tryparse(string, boolean, tenum). both methods allow parse input in case-insensitive manner and parse both values or names.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -