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

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

c++ - libcurl curl_easy_setopt "Unknown error" -

protocol buffers - zeromq with protobuf segmentation fault while parsing in c++ -