0

I was wondering how to go about writing a custom attribute which can access both the input and output parameters of a function.

Below is an example of what I wish to achieve

[CustomAttribute("Creating Foo")]
public Foo CreateFoo(Foo newFoo)
{
    //do logic
    return newlyCreatedFoo;
}

From this, the CustomAttribute will create an entry in a DB with the "Creating Foo" tag, which is easy enough to do, but I want to be able to access both newFoo and newlyCreatedFoo as well. Is this possible?

3
  • Do you want to access the values or the definition of the parameters? The first is impossible (attributes don't intercept calls), the second is possible using reflection from any code. You don't need an attribute to do that Commented Nov 30, 2012 at 14:29
  • unfortunately it is the values required Commented Nov 30, 2012 at 14:31
  • You are talking about interception and injection then. Nothing to do with attributes per se Commented Nov 30, 2012 at 14:32

3 Answers 3

4

Custom attributes can't do that in general: their purpose is to add metadata to a method to view during reflection.

That being said, there are some cases where attributes are leveraged to do that. For example, ASP.Net MVC uses custom attributes that implement IAuthorizationFilter to provide security for some web pages. This works because ASP is using reflection to launch the methods in the first place. Once it gets the method it checks to see if any attributes are IAuthorizationFilters, and does some extra work when they are. See this link for some more info.

Another way to think about this is to consider aspect-oriented programming. I think AOP frameworks for c# tend to make compile time decorations to methods based on attributes that implement a certain interface, but I have not used one.

My favorite way to deal with this is the good old Proxy pattern. Create a logging proxy.

Sign up to request clarification or add additional context in comments.

Comments

1

I am afraid that this is not possible with custom attributes.

This could be achieved with post compilation processing. Tools like PostSharp would allow you to achieve this functionality.

8 Comments

Why? This is nothing more than a comment.
@casperOne, did you read the last line of the OP? I quote: Is this possible?. Now read my answer once again. The OP asks whether this is possible and the answer is no.
I was afraid this may be the answer. I don't suppose you would know of any alternatives to achieve something similar?
I read it. But we can do better than this. An explanation of why would be a much better contribution to the site. Taking narrowly-constrained view on a question such as this and answering in the narrowest way possible without any further background for the benefit of easy reputation is not the impression that Stack Overflow wants to give.
@Thewads, there might be some alternatives but you will have to provide more details about your scenario and why you need such functionality.
|
0

Perhaps you should rephrase your question to "How can I intercept calls to specific methods and log the arguments"?

A custom attribute is just a decoration on a method. It does not intercept or in any way affect the execution of a method. It doesn't even know what member it is attached to.

If you want to intercept the calls you can use a dependency injection framework, or any other AOP framework for .NET to do just that. Some of them actually use attributes to mark their targets, but that isn't a requirement.

PostSharp is just one AOP framework for .NET.

Another option, is to use an IoC/Dependency Injection library like MEF to wrap your class in a proxy object that will intercept all calls and only log the values of the methods decorated with a special attribute.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.