c# - How can I read method parameter information (names and values) from an attribute class in .NET -


I am creating a cool custom HTTP client clip that is using properties to create dynamic routes is. How to use method parameter values ​​from the properties of the method. Here is an example of a method with my custom attribute: [RestEndpoint ("Appointment / {Appointment IIT}")] GetAppointmentById (integration IID) {// code call for public placement} defined in the attribute of this method in the base class manner Press the end point}

Below is my specialty class. In my attribute class, I want to read the parameters of the method connected to the gate-dynamic and point () method, I am trying to make space and Reggae sugar.

I can not seem to understand how exactly the information of the law has been received, which is linked to a given attribute. I can do the opposite (read the method attribution information). [Attribute_use (attributeTargets.method, Inherited = false, Allow = Multilevel)] Public Sealed Class RestEndpoint: Attribution {Public HttpVerb Verb {Received; Set; } Public string {received; Set; } Public Restpointpoint (string yury) {verb = http webb. Uri = Yuri; } Public Relaxation Point (HTPVRB verb, String Yuri) {verb = verb; Uri = Yuri; } Public string GetDynamicEndpoint () {// Get the method for this attribute and read the parameter "dynamic end point" in order to create the dynamic endpoint based on the parameter values ​​of the method; }}

So from comments and research, what am I trying to do here It is impossible with existing built-in Net Framework capabilities. However, I have consulted Eugene and have passed the parameter in this parameter to create a dynamic path. Something finished this way:

  [UseRestEndpoint ("Appointment / {first} / {last}")] Public appointment count GetAppointmentById (string first, last string) {return Send < AppointmentDto & gt; New (first, last)); }  

and the specialty call that creates the dynamic path uri in the dynamic object:

  public string GetDynamicEndpoint (dynamic parameter) {if (! Uri.Contains ("{"} and Uri. Content ("}"); Back; Var valueDictionary = GetUriParameterValueDictionary (parameter); String NewURI = Uri; Newacha = newUri.Replace (string.format ("{{{0}}}, pair.kA), pair.value); new urai return;} Private dictionary & lt; string, string & gt; GetUriParameterValueDictionary (object parameter) {var propBag = parameters.ToPropertyDictionary (); Return GetUriParameters (). ToDictionary (s = & gt; s = s => propBag [s]) ;} Private IEnumerable & lt; String & gt; GetUriParameters () {Regex regex = New Regex ("" ​​(? & Lt; = {} * w * (? =}) "); Var MatchCalue = Rex.MAT (Uri ); Return (Select match m from Milan match m.Value) .Oolist ();}  

It does not have all this implementation code to work, but the concept is finished to work. Thanks to everyone for comments.


Comments