Custom Attributes
Tuesday, 22 June 2010
Article Index
Custom Attributes
Attribute semantics
Initialising fields
Practical attributes
An example
Solving the practical problems

Banner

In principle we could pass any struct or class to the method and there really isn’t a type-safe way of doing this without using generics and this would complicate the example.

However as long as we actually test the type passed to the method this should at least be runtime type-safe. If you want to restrict what can be passed to structs you can, using:

static public void display(ValueType a)

Unfortunately this also allows simple types such as int and long to be passed so you need to add another test.

The problem is that struct isn’t a type, it’s a keyword that creates a struct which inherits directly from ValueType as do all simple types. This means you can’t separate all structs from the rest of the ValueTypes. You can’t even simply test to see if what has been passed in has fields to determine if it is a struct because simple types like int also have fields!

Moving on to provide the mechanism for inspecting and processing the attributes, the first step is to get all the fields:

System.Reflection.FieldInfo[]
fields= a.GetType().GetFields();

Next we step through the array and process each field in turn:

foreach (System.Reflection.FieldInfo 
field in fields)
{

We need the array of Formattable attributes that have been applied and in this case we also need the value of the field:

Formatable[] formats=(Formatable[])field.
GetCustomAttributes(typeof(Formatable),
 false);
int temp = (int)field.GetValue(a);

Notice that we should also check that the type of the field is suitable for the formatting about to be applied but this has been omitted for simplicity.

Now we can use the Formattable attribute object to determine what format string to store in format:

string format="";
if (formats.Length!=0)
{
if (formats[0].format==FormatType.Money)
{
format="C";
}
}

Notice there can only be one Formattable object because of the:

AllowMultiple = false

Finally we can display the result and close the foreach loop, method and class:

  }
MessageBox.Show(temp.ToString(format));
}
}

Now you can write:

MyData SomeData = new MyData();
SomeData.cost = 123;
SomeData.notacost = 456;
Formatter.display(SomeData);

The cost field, tagged as money format, will display with a currency symbol and the notacost field will display without. Unfortunately as it stands the display method also accepts simple types so:

Formatter.display(1);

displays the two fields associated with a boxed int. There seems to be no elegant way of stopping this from happening.

One solution would be to require the Formattable attribute to be applied to the entire struct before any of its fields are processed. That is, change the attribute to

[AttributeUsage(AttributeTargets.Field|
AttributeTargets.Struct,
AllowMultiple = false,
Inherited = false)]
public class Formattable : Attribute

add a constructor that takes no parameters and add a test that the passed-in parameter did indeed have a Formattable attribute before processing it.

Finally, a new feature introduced in C# 3.0 makes attributes more useful. The static method that implements the machinery would be better associated with the struct or, in general, the types to which  it applies. The extension method facility can be used to retrofit methods to classes that already exist. You can even add new methods to build in types such as int or string.

Unfortunately the problem that structs inherit directly from ValueType makes it impossible to add an extension method to all structs and nothing else. You can easily add an extension method to a single named struct but why bother… you might as well just add the method to the struct directly. To add the display method to all value types you simple change its definition to:

static public void display(
this System.ValueType a)

That is, add the modifier “this” to the first parameter. Now you can call the display method using:

SomeData.display();

In other words, the display method has been added to every struct you create – powerful isn’t it! Unfortunately it has actually been added to every value type so you can also write:

int i = 10;
i.display();

which is perhaps not what you intended. In this case it will display the two fields supported by a boxed int i.e. the maximum and minimum values.

Conclusion

Attributes are something that you probably won’t use everyday, but now that you know exactly how they work you can spot when the approach might be useful.

 

Banner


What's The Matter With Pointers?

Back in the days when C was the language of choice, pointers meant programming and vice versa. Now in the more sophisticated and abstract days of C#, and even C++, raw pointers are a facility that is  [ ... ]



C# Bit Bashing - The BitConverter

Is C# a high-level or a low-level language? It doesn't really matter - all languages are low-level when you are thinking in terms of bits, and sometimes you just can't avoid thinking in bits.


Other Articles

<ASIN:1449380344>

<ASIN:0123745144>

<ASIN:0321658701>

<ASIN:0321741765>

<ASIN:0470495995>

<ASIN:0672330792>

<ASIN:0521114292>



Last Updated ( Friday, 27 August 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.