Use Lambda in Attribute constructor to get method's parameters

Posted by Anthony Shaw on Stack Overflow See other posts from Stack Overflow or by Anthony Shaw
Published on 2010-06-16T01:23:23Z Indexed on 2010/06/16 1:32 UTC
Read the original article Hit count: 301

Filed under:
|
|

I'm not even sure if this is possible, but I've exhausted all of my ideas trying to figure this out, so I figured I'd send this out to the community and see what you thought. And, if it's not possible, maybe you'd have some ideas as well.

I'm trying to make an Attribute class that I can add to a method that would allow me to use a lambda expression to get each parameter of the method


public ExampleAttribute : Attribute 
{
    public object Value { get; set; }

    public ExampleAttribute(--something here to make the lambda work--, object value)
    {
        Value = value;
    }
}

I'd like to be able to something like this below:


[Example(x=>x.Id, 4)]
[Example(x=>x.filter, "string value")]
public ActionResult Index(int Id, string filter) 
{
    return View();
}

I understand that I might be completely dreaming with this idea. I'm basically trying to write a model to allow for self-documenting REST API Documentation. In a recent project here at work we wrote a dozen or so services with 5 to 15 methods on each, I figure it's easier to write something to do this, than to hand code a documentation page for each. I would plan to eventually release this as an open-source project once I have it in a place that I feel it's releasable.

© Stack Overflow or respective owner

Related posts about c#

Related posts about attributes