c# - Get Method by CustomAttribute and then Invoke -
with massive learning curve custom attributes , reflection, still seem struggling. me out one?
basically want invoke method based on it's attribute.
this customattribute decorates method (it ever 1 method): [controllername(name="blog")]
public static string getcontent(string controllername, string singleitemname = "") { string returnval = null; //get class type type thistype = typeof(contentfacade); //get member info system.reflection.memberinfo info = typeof(contentfacade); loop through attributes foreach (object attrib in info.getcustomattributes(true)) { //if attribute matches param controller name //then invoke if (attrib == controllername) { //get method attribute not method name //i dont want method methodname methodinfo themethod = thistype.getmethod(controllername); //return value html end user returnval = (string)themethod.invoke(controllername, null); } } return returnval; }
again, if possible method attribute.
really looking forward on this.
///edits sorry added comments in code illustrate issue. to:
- get attribute controllername == "blog"
- get method attribute , invoke
i don't want method method name.
regards,
a simple example be:
public static string getcontent(type type, string controllername) { foreach (methodinfo method in type.getmethods(bindingflags.static | bindingflags.public)) { controllernameattribute controller = (controllernameattribute)method.getcustomattribute(typeof(controllernameattribute)); if (controller == null) { continue; } if (controller.name != controllername) { continue; } if (method.isgenericmethod) { throw new invalidoperationexception(); } if (method.getparameters().length != 0) { throw new invalidoperationexception(); } if (method.returntype != typeof(string)) { throw new invalidoperationexception(); } string result = (string)method.invoke(null, null); return result; } throw new invalidoperationexception(); }
you use like:
string result = getcontent(typeof(program), "blog");
where program
class method should be.
note that, written, code work on static
methods. if want use instance methods, change to:
public static string getcontent(object instance, string controllername) { type type = instance.gettype(); foreach (methodinfo method in type.getmethods(bindingflags.static | bindingflags.public)) { controllernameattribute controller = (controllernameattribute)method.getcustomattribute(typeof(controllernameattribute)); if (controller == null) { continue; } if (controller.name != controllername) { continue; } if (method.isgenericmethod) { throw new invalidoperationexception(); } if (method.getparameters().length != 0) { throw new invalidoperationexception(); } if (method.returntype != typeof(string)) { throw new invalidoperationexception(); } string result = (string)method.invoke(instance, null); return result; } throw new invalidoperationexception(); }
in general, both methods don't check if there single method given controllername
. execute first 1 found. possible checking, using linq, wanted keep foreach
cycle simple , linear, , show other checks should done.
Comments
Post a Comment