C#: Attributing source-code via header comments. An alternative by using custom attributes and c-sharp runtime-reflection

Upon glancing this rather unpopular and unnoticed post on StackOverflow I felt like raising a tiny bit of deserved attention by adding my own two cents. Its wider premise is simple:
Once code is written and reasonably working, its journey completes by hitting a long-term, code revisioning bit-bucket, which ideally isn't dev/null-as-a-service
What generally ensues in the final stages of a code-repository is a bit of overdue code cleanup, refactoring if beneficial to future code-reuse and naturally proper code headers/signing and attribution. In case the code is written as part of a bigger team, you generally won't get to choose much of the code-styles, documentation guidelines, utils and what you may or may not omit.

But in case of private projects, you can afford to pay due diligence to details that are of concern to you, yet may result in all but a lethargic shoulder-shrug in others.
Showing a method for attributing c# source-code with metadata

Without further ado, here is what I have come accustomed to. Keep in mind that attributes are essentially stored as Assembly metadata, and are only accessible through Runtime-reflection, or an Assembly/CIL reader like ILSpy.



As +Darin Dimitrov points out, Attributes are:


  • metadata. 
  • baked into the assembly at compile-time which has very serious implications of how you could set their properties. Only constant (known at compile time) values are accepted
  • The only way to make any sense and usage of custom attributes is to use Reflection. So if you don't use reflection at runtime to fetch them and decorate something with a custom attribute don't expect much to happen.
  • The time of creation of the attributes is non-deterministic. They are instantiated by the CLR and you have absolutely no control over it.


Note: 

As most of you know, C# brought with it, the recommendation of using XML-style comments for document generators. As decades passed, XML's semi-vision as a human readable container format diminished. It does not feel natural to write or to read, and being commented out doesn't ameliorate the situation.

It is important to note that this post is not about a right or wrong answer/solution when it comes to stamping your source. Use what works best for you and whatever workflow you are most accustomed to. But if you have yet to decide upon a methodology or are shopping around for alternatives, this post may hopefully incite your imagination.
LihatTutupKomentar