Imagine I have the following code:
class A
{
[UsefulAttribute("foo")]
B var1;
[UsefulAttribute("bar")]
B var2;
...
}
class B
{
public string WriteSomethingUseful()
{
?????
}
}
My question is, what code do it need to put in the ????? such that, when I call var1.WriteSomethingUseful I get an output of foo, and when I call var2.WriteSomethingUseful I get an output of bar?
I've got a feeling this is quite a straightforward question, I think my main issue is that I have worked myself into a state of confusion by thinking about it for too long!!!
Seriously, I have defined UsefulAttribute and realise that part of the code must be a GetCustomAttributes(typeof(UsefulAttribute)...) call. Where I'm getting confused is how to pull these values out on the actual instance, rather than at the type level.
Many thanks, Pete
WriteSomethingUseful(FieldInfo attributeContext). I'd work out a full solution for you but I don't have a Windows dev box at the moment. Regardless, best of luck.