c# - Call JsonResult by string name -


i have controller has 50+ private functions return jsonresult type. i'm trying create generic jsonresult accepts name string corresponds 1 of 50+ private jsonresult functions.

i want this:

    public jsonresult view(guid id, string funcname)     {                    return redirecttoaction(funcname, "mycontroller", new { id = id});     } 

but won't work because "redirecttoaction" returns actionresult, not jsonresult. there way make work?

you can use reflection:

public actionresult view(guid id, string funcname) {                return (actionresult)this.gettype().getmethod(funcname).invoke(this, new object[]{id}); } 

as mentioned in comments. if private method need add binding flags:

return (actionresult)this.gettype().getmethod(funcname, bindingflags.nonpublic | bindingflags.instance).invoke(this, new object[]{id}); 

Comments

Popular posts from this blog

rdbms - what exactly the undo information lives in oracle? -

bash - How do you programmatically add a bats test? -

clojure - 'get' replacement that throws exception on not found? -