I recently implemented a ValueConverter for our comboboxes in silverlight, whereby the translations for an enum would be shown as a list in the combobox, and when an item is selected, the value bound to the model would be whatever was described on the enumeration as the model value that gets persisted to the database (it’s a legacy database, so I couldn’t save the enumerated value on the model). Anyways, more on how that was done is for another blog post.
The converter gets an enumeration type as a converter parameter, so I needed to construct the type of enum at runtime (so I could get access the the enumerations that were defined, and the ModelValue attributes on each enumeration definition), since the converter should be able to handle any enumeration type that has been defined according to a specific convention. Trouble is, the enumerations were defined in a seperate silverlight assembly that was referenced from the client assembly, so I couldn’t just simply:
… only if you specify the culture and the version in the above string parameter, will the type get returned, and since those change frequently, it wasn’t a practical solution.
So here’s helper method I wrote to get a type referenced in another assembly:
… if you know the fully qualified class name and which assembly it’s in:
… or if you only know the fully qualified class name, and don’t know which assembly it’s in:
Notice that in each case that the class name that is specified is fully qualified with it’s namespace.
Happy Coding :)