c# - Extendable architecture to create subclass objects based on a string? -


consider form user can upload file.

i @ mime type sent file (e.g. application/pdf) , want construct object of appropiate type.

for pdf object of pdfdocument inheriting abstract document class.

how can build architecture extendable additional "mime string class" mappings? want make easy add classes new mime types without having change existing code handles uploaded document.

the naive implementation this:

public document getdocumentbymimetype(string mimetype) {     switch(mimetype)         case "application/pdf":              return new pdfdocument();         case "application/msword":              return new worddocument();         ... } 

i have touch code every time want add new mimetype document subclass.

i using unity ioc framework, can solve problem registering types somehow @ startup? there maybe way register types automatically adding subclasses assembly?

and best way resolve class mimetype in above method?

have considered using system.activator.createinstance()?

these methods allow create instance of object @ runtime type or name of class.

as such can maintain dictionary of classes or have database/config file loaded set can used create instance of required object.

for example msdn,

objecthandle handle = activator.createinstance("personinfo", "person"); person p = (person) handle.unwrap(); p.name = "samuel"; console.writeline(p); 

creates instance of person personinfo dll.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -