c# - Get and save enum using reflection -
i have project in have assemblies implement abstract class.
each assembly has public enum called resultenum. resultenum's value stored in database int.
i have web project displays info, , want display int's string representation - name of corresponding value resultenum.
what want is, using mef, load relevant assemblies (no problem here), search enum using reflection (no problem here also) , store enum in way, , cache in order avoid process next time want convert int database string representation (and other way around if necessary) since have several thousands of records in db table.
aggregatecatalog catalog = new aggregatecatalog(); catalog.catalogs.add(new directorycatalog(path)); _container = new compositioncontainer(catalog); try { _container.composeparts(this); } catch (compositionexception compositionexception) { console.writeline(compositionexception.tostring()); } foreach (var task in mytasks) { taskabstract instance = (taskabstract)task.createexport().value; memberinfo[] infos = instance.gettype().getmembers(bindingflags.public | bindingflags.static); foreach (memberinfo member in infos.where(x => x.name.equals("resultenum"))) { console.writeline(member); } }
what suggest next move should be? how should store/cache it?
thanks
in addition @thomas's answer:
as using reflection can exact int value property, name concrete value gotten using next expression:
var enumvaluename = enum.getname(member.gettype(), member.getvalue(instance));
upd
i missed reflect memberinfos. apply solution can update reflection way:
foreach (var task in mytasks) { taskabstract instance = (taskabstract)task.createexport().value; // reflect properties, not members propertyinfo[] infos = instance.gettype().getproperties(bindingflags.public | bindingflags.static); foreach (propertyinfo prop in infos.where(x => x.name.equals("resultenum"))) { var enumvaluename = enum.getname(prop.gettype(), prop.getvalue(instance)); } }
or cast memberinfo propertyinfo.
Comments
Post a Comment