views:

106

answers:

3

Are there any good resources out there that explain attributes and reflection and their many uses in depth?

I am especially interested in best practices, i.e. when it would be useful or appropriate to use attributes and reflection to solve specific computing problems.

+1  A: 

I don't think there are any general resources that would provide you with what you are looking for. Overall, attributes are simply a way of adding metadata to code, without changing that code's behavior. In and of themselves, attributes do not do anything. Attributes let you plug your code into something else, and that other thing can utilize the additional metadata to provide improved functionality.

A couple good examples are the Visual Studio Design Surface, and WCF. Using attributes like [Description], [Category], etc. don't actually do anything to your code...but they do let the Visual Studio designer display and organize your objects properly. Similarly, the [ServiceContract], [OperationContract], [DataContract], [DataMember], and other WCF attributes don't make your code become a web service...they simply make it possible to plug your code into WCF, which in turn makes it a web service.

Attributes are a metadata injection mechanism, and nothing more. Other programs, frameworks, or utilities that USE that metadata are what give attributes their true power. There aren't really any "best practices" about when to use an attribute or not. They are just a tool. They enable you to annotate your code with useful information, and use that information somewhere else. The key is knowing when you need additional metadata...and that is highly subjective...only the person designing the architecture of the project will be able to determine if new custom attributes will be needed.

If you have a specific scenario where you think you need attributes, update you're answer with an example, and I'll see what additional help I can offer.

jrista
Here is a good example: http://stackoverflow.com/questions/1482135/custom-authorizeattribute. The OP in this question is creating a custom attribute to validate the existence of a record in the database. I answered his question by stating that it might be better to check in the method body, because the attribute requires specialized knowledge (the record id) which it has to get somewhere else, information which the method body already has.
Robert Harvey
I liked the idea of a custom attribute for role-based security, because that's how the security system works in ASP.NET MVC. I thought about creating custom attributes to validate that the user has access to certain projects (i.e. projectID), but using such an attribute would require that the attribute have knowledge of the projectID. It can get this from the HTTP Request, but it seems messy; it introduces another dependency to get the ProjectID, and the routing engine has ALREADY provided the control method with the required project ID
Robert Harvey
You have to keep in mind...creating and adding a new attribute does NOTHING in and of itself. In all contexts where an attribute "does something", it is infrastructural code, such as ASP.NET MVC, WCF, Visual Studio, an OR Mapping framework, etc. that is getting the attribute and the metadata it represents, and doing something with it. Attributes themselves are useless without additional boilerplate to make them work.
jrista
Good point. Thanks for the info.
Robert Harvey
+2  A: 

I don't know of a good comprehensive resource. In my experience, the best way to learn is to run into a situation that can't be solved without it (at least not easily). So if you know the basic situations where reflection & attributes make things easier, you'll know when you need to start learning them.

Reflection: appropriate when you can't do what you need without it.

I know that sounds a little snarky, but it's actually true - if you need to write some code but there is just no way to figure out the types of the objects you're dealing with, reflection is appropriate. It's also appropriate when you need to sneak in and use private/protected/internal members of an object you don't control.

Attributes: appropriate when you need to add extra information about a type.

Example: I have a bunch of IConverter classes. Each one corresponds to a specific field in our content management system. How can I decorate this class with extra information which describes the field it is used for?

  • I could create a different interface for every converter, even though they all do the same thing, but that seems like a bad idea.
  • I could put a read-only property that returns a hard-coded string to the name of the field on each class, but then I'd have to create an instance of each one to check the property value.

It would be nice if I could just put some kind of extra piece of information on the class itself that says what field it's for. Then I can check through all my types and find the one I want, and just instantiate that one. Attributes let me do that:

[HandlesField("FieldName")]
public class FooFieldConverter : IConverter
Rex M
Thanks for that.
Robert Harvey
A: 

The following article is a good primer:

Designing With Custom Attributes
http://msdn.microsoft.com/en-us/magazine/cc163802.aspx

Robert Harvey