Amily Bennet
Amily Bennet

Reputation: 1

Querying C# attribute

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.

Upvotes: 0

Views: 100

Answers (1)

Ameen
Ameen

Reputation: 2586

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.

Upvotes: 1

Related Questions