Skip to content

Instantly share code, notes, and snippets.

@lsauer
Created November 27, 2013 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsauer/7683528 to your computer and use it in GitHub Desktop.
Save lsauer/7683528 to your computer and use it in GitHub Desktop.
C#: source-code header comments alternative by using attributes and runtime-reflection
//www.technical-programming.com, 2013 lo sauer
using System;
namespace MyProject.Attributes
{
public enum CodeAttributeName {
Author,
Copyright,
RevisionHistory,
License,
Buildnumber,
Buildstatus,
//add your own...
}
//Use as follows
//[CodeAttribute()]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)]
public class CodeAttribute : Attribute
{
private CodeAttributeName name;
public object value;
public CodeAttribute(CodeAttributeName attributename, object attributevalue = null)
{
this.name = attributename;
this.value = attributevalue;
}
}
}
//www.technical-programming.com, 2013 lo sauer
//Example Usage
[CodeAttribute(CodeAttributeName.Author, "lorenz.lo.sauer@gmail.com"),
CodeAttribute(CodeAttributeName.Author, "others credited in the code")]
[CodeAttribute(CodeAttributeName.Copyright, "lo sauer,2013")]
[CodeAttribute(CodeAttributeName.License, "MIT Licence")]
[CodeAttribute(CodeAttributeName.RevisionHistory, @"
ls,13-08-2011,0.11: Windows Shell Implementation
ls,13-11-2011,0.12: Fixed Win32API Message Hook
ls,13-11-2011,0.12: Fixed Dictionary Key Access
ls,13-11-2011,0.12: Added Markov(2n), Markov(3n) Methods for Entropy Calculation
")]
public partial class Program
{
/// <summary>
/// Main entry point, short one-line description blabla...
/// </summary>
[STAThread]
static void Main()
{
Console.WriteLine("Hello World #2312");
Console.WriteLine( ((CodeAttribute)Attribute.GetCustomAttribute(typeof(Program), typeof(CodeAttribute)))
.RevisionHistory.ToString()
);
}
}
// <code-header>
// <author>lorenz.lo.sauer@gmail.com</author>
// <copyright>lo sauer,2013</copyright>
// <license>MIT Licence</license>
// <revisions>
// <revision initials="ls">Windows Shell Implementation </revision>
// <revision intiails="ls">Fixed Win32API Message Hook</revision>
// <revision intiails="ls">...</revision>
// </revisions>
// </code-header>
public partial class Program
{
//..
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment