Use Lambda in Attribute constructor to get method's parameters
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.
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.
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.
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
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
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:
- Open the project's Property Pages dialog box.
- Clickthe Configuration Properties folder.
- Click the Build property page.
- 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.
精彩评论