c# - Make controller more efficient by choosing method dynamically -


i have these 2 methods in repository:

    public ienumerable<post> posts     {         { return context.posts; }     }       public ienumerable<editables> editables     {         { return context.editables;      } 

here method:

 public actionresult saveedit(int activeid,string text, string prop, string repomethod)         {             var elementtoedit = _repository.<dynamic insert post or editable>.elementat(activeid);             var type = elementtoedit.gettype();             propertyinfo p = type.getproperty(prop);             p.setvalue(elementtoedit, text, null);              _repository.updatebiglinkedit(elementtoedit, activeid);              return null;         } 

so, ii possible make methid more dynamic , insert right method

_repository.editables.elementat(activeid)  or  _repository.post.elementat(activeid); 

by passing name of nethod view? or have go way?

yes can, using generic method:

public t getelement<t>(int id) {   if (typeof(t) == typeof(post)) return posts.elementat(id);   if (typeof(t) == typeof(editable)) return editables.elementat(id);   throw new invalidtypeexception(); } 

and use

var element = _repository.getelement<post>(activeid); 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -