views:

185

answers:

5

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.

A: 

It would be great if you could pass delegates as parameters to attributes. The possibilities are endless. Unfortunately, it is not currently possible but it is on the radar.

Brian Gideon
that will be awesome when/if they implement.. thanks for the link :)
Anthony Shaw
+2  A: 

I don't believe it's possible, but I don't think you need to anyway. Instead, you can put the attribute directly on the parameters, like so:

public ActionResult Index(
    [Documentation("the identifier...")]
    int id,

    [Documentation("The filter")]
    string filter
  )
{
    return ...;
}

You can then use ParameterInfo.GetCustomAttributes to get the attributes on the parameters.

Dean Harding
thank you, I forgot that you could apply attributes to the parameters themselves.
Anthony Shaw
A: 

Why reinvent the wheel (for documenting at least)?

Microsoft has a standard documentation structure, XML documentation, that can be compiled into .chm documentation files.

Using the triple /// notation to document your methods:

   /// <summary>MyMethod is a method in the MyClass class.
   /// <para>Here's how you could make a second paragraph in a description. <see cref="System.Console.WriteLine"/> for information about output statements.</para>
   /// <seealso cref="MyClass.Main"/>
   /// </summary>
   public static void MyMethod(int Int1)
   {
   }`

you can then use the <param name='Int1'>This is an int.</param> xml to define any parameter values.

Once you have documented your class and methods, you can the use Sandcastle to compile it into html help files.

To generate the XML files:

  1. Open the project's Property Pages dialog box.
  2. Clickthe Configuration Properties folder.
  3. Click the Build property page.
  4. Modify the XML Documentation File property.

To help you, I recommend a tool called GhostDoc, which provides context-menu documentation generation for a method. It doesn't provide a perfect documentation, but it provides a good general structure.

Combined with Sandcastle, XML documentation is a great tool for documenting code and assemblies.

Alastair Pitts
this won't be a normal documentation, it will allow to create forms to pass values to test the methods. It's more of a documentation/test form at the same time. This way, if you have third-party developers utilizing your services, this will allow them to see what they should be receiving back from the service before implementing it.
Anthony Shaw
Ah. Fair enough. I wasn't aware of the double requirement. Good question though :)
Alastair Pitts
A: 

To my knowledge, the attribute parameters are limited to bool, byte, char, double, float, int, long, short, string, object, System.Type and enum type with public accessibility and single dimensional array of the above types.

MSDN Reference

Kthurein
A: 

No this is not possible. The list of allowed values is covered in section 17.2 of the C# language spec. It is limited to

  • Constant values
  • A System.Type object
  • A one-dimensional array of either of the above

Lambda expressions don't fit any of these categories

JaredPar