0

i have tried to surf the internet but i could not get anything related to what i want.

This is in relation to ASP.Net. But could be any other instance as well.

Following is my attribute

class  SomeAttribute :Attribute
{
    string someparam;
    string SomeParam
    {
         get{ return someparam;}
         set { someparam = val;}
         //the value generated for someparam is dynamically generated with respect to some attribute present in the request header.
    }
 }

it's similar to the [Authorize] attribute that .net uses in its asp .net memberships to validate if the user has logged in and it redirects him back to log in page if validation fails.

I have an attribute associated with a method like below:

   [SomeAttribute] 
     public void someFunction
    {
      //i want to retrieve here the value of someparam jus generated before entering this method.
     }

Note that i don't pass any value or used any named properties in this attribute. It is simply going to check a condition for me whenever the method is called and return true or false and accordingly the function is either called or not.

In my case, After validating, it generates a value and that value has to be shared with the function to which it is associated 'somefunction'.

i know reflections can help me get the attributes associated with a function and the value of its properties.

But here i dont want to fetch the value from some other function. And i dont want to just fetch the attribute either.

As i mentioned earlier when the function is called the attribute will work upon that. What the attribute does is fetches some data from request header and does some processing and generates a value. Now this value has to be passed on to the function just after that.

  • Attributes are build when you compile it and you can change it on run time. Check this http://stackoverflow.com/questions/2311719/is-net-attribute-feature-used-at-compile-time-or-run-time-or-both for more information about. – nozari Aug 07 '13 at 03:24
  • you can use session or the request object to pass the value. – nozari Aug 07 '13 at 03:24
  • I don't understand your question "but here will the values of the properties be maintained...". Would you mind clarifying? Also, what are you asking. What do you want to know if it's possible or not? – Patrick Aug 07 '13 at 03:37
  • Also if you aren't doing any magic with your Properties I would use AutoProperties: string SomeParam {get; set;} and not use the string someparam; – Cubicle.Jockey Aug 07 '13 at 03:40
  • cubicle - well yes i can use {get; set;} ... i'l make a note of that – Amily Bennet Aug 07 '13 at 05:14
  • Patrick - i have edited the question. – Amily Bennet Aug 07 '13 at 05:15
  • @Amily Put an @ before the user's name in order to alert them to your response, like I've just done here. – doppelgreener Aug 07 '13 at 05:21
  • @Patrick - to be more specific what i mean is after the attribute acts upon the method i want to be able to propagate a property from the attribute to the same method at the same instance... – Amily Bennet Aug 07 '13 at 05:28

1 Answers1

1

Well, what you want to accomplish is certainly possible, but it would not be an optimal use of the run-time or the MVC model.

In this particular case, think of an attribute as an annotation. It's something you use to mark a function, controller, etc. so that its execution behaves differently at run-time. The attribute itself should not be doing the bulk of the work, rather just signalling to other pieces in your design to behave differently.

It seems like you want to check some header values, and calculate something based off of that. You can use extension methods off of a Request class to accomplish this.

Now, let's say that inside your Controller's function, you want to guarantee that the header values exist. You can have an attribute called RequireHeaderValues that implements IActionFilter. The attribute would check the header for the required values and if they don't exist, it routes the response to another location or somehow indicates error. This way, when inside your function, you call the extension method off the Request object, you are guaranteed that the values will exist. The MVC run-time will automatically call your filter attribute for you. For more info, see this.

Ameen
  • 2,576
  • 1
  • 14
  • 17