c# - How to use enums with dynamic and word -
in function load template using word , replace words other words. that's not important. how change function use dynamic
? in particular, how use enums (such wdreplace.wdreplaceone) without having reference microsoft.office.interop.word?
public static void createsheetbyreplacement(string student, list<string> words) { application wordapp = new application(); wordapp.documents.add(environment.currentdirectory + "\\spellingsreplace.dot"); wordapp.selection.find.execute(findtext: "studentname", replace: wdreplace.wdreplaceone, wrap: wdfindwrap.wdfindcontinue, replacewith: student); (int = 0; < 80; i++) { string word = (i < words.count ? words[i] : ""); wordapp.selection.find.execute(findtext: "[word" + (i + 1) + "]", replace: wdreplace.wdreplaceone, wrap: wdfindwrap.wdfindcontinue, replacewith: word); } wordapp.visible = true; wordapp = null; }
i need 1 example of how use enums.
while there no way use enums such dynamic
late binding other directly using respective integer values, can use reflection , expandoobject
build dynamic lookup object:
public static class dynamicinterop { public static dynamicinterop() { var enumsdict = new expandoobject() idictionary<string, object>; // enum types interop assembly var interopenums = getinteropassembly() .gettypes() .where(type => typeof(enum).isassignablefrom(type)); // enum types create member in enums dynamic object foreach (var type in interopenums) { var curenum = new expandoobject() idictionary<string, object>; // enum value name , values keyvaluepairs var enumkeyvalues = enum .getnames(type) .zip(enum.getvalues(type).cast<object>(), (key, value) => new keyvaluepair<string, object>(key, value)); // create members every enum value name-value pair foreach (var keyvalue in enumkeyvalues) { curenum.add(keyvalue.key, keyvalue.value); } enumsdict.add(type.name, curenum); } dynamicinterop.enums = enumsdict; } public static dynamic createwordapp() { throw new notimplementedexception(); } public static dynamic enums { get; private set; } }
while such approach may not suit needs @ least reduce probability of passing wrong enum value.
p.s.: not tested, there may few typos or other errors.
p.s.1: still, no intellisense , other ide , compiler assistance late binding interop dynamic
can become difficult part of code maintain.
Comments
Post a Comment