Reputation: 180798
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.
Upvotes: 3
Views: 384
Reputation: 180798
The following article is a good primer:
Designing With Custom Attributes
http://msdn.microsoft.com/en-us/magazine/cc163802.aspx
Upvotes: 0
Reputation: 144122
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?
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
Upvotes: 3
Reputation: 32960
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.
Upvotes: 1