Deep C# - Custom Attributes In C#
Written by Mike James   
Friday, 19 March 2021
Article Index
Deep C# - Custom Attributes In C#
Attribute Semantics
Practical Attributes
Using Attributes
An Example
Conclusion

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.

The static method that implements the machinery would be better associated with the struct or, in general, the types to which it applies i.e. it should be a method of the type is is going to display the fields of. 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 is 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.

 

Related Articles

In search of default properties

 

Deep C#

 Buy Now From Amazon

DeepCsharp360

 Chapter List

  1. Why C#?

    I Strong Typing & Type Safety
  2. Strong Typing
       Extract 
    Why Strong Typing
  3. Value & Reference
  4.    Extract Value And Reference
  5. Structs & Classes
       Extract
    Structs & Classes 
  6. Inheritance
      
    Extract
    Inheritance
  7. Interfaces & Multiple Inheritance
      
    Extract Interface
  8. Controlling Inheritance
    II Casting & Generics
  9. Casting - The Escape From Strong Typing
      
    Extract Casting I ***NEW!
  10. Generics
  11. Advanced Generics
  12. Anonymous & Dynamic
    Typing
    III Functions
  13. Delegates
  14. Multicast Delegates
  15. Anonymous Methods, Lambdas & Closures
    IV Async
  16. Threading, Tasks & Locking
  17. The Invoke Pattern
  18. Async Await
  19. The Parallel For
    V Data - LINQ, XML & Regular Expressions
  20. The LINQ Principle
  21. XML
  22. LINQ To XML
  23. Regular Expressions
    VI Unsafe & Interop
  24. Interop
  25. COM
  26. Custom Attributes
  27. Bit Manipulation
  28. Advanced Structs
  29. Pointers 

Extra Material

 <ASIN:1871962714>

 <ASIN:B09FTLPTP9>

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 



Last Updated ( Friday, 19 March 2021 )