• Bug Fixing and Tracking with Reflection

    by  • January 29, 2012 • .Net, Programming • 0 Comments

    If you ever meet a programmer that tells you that they code everything perfect first time and don’t need to do any testing or bug fixing, you should slowly roll up a newspaper (if you have one handy) and hit them over the head while saying ‘No ….no’. There is a tiny amount of code that is written correctly the first time.

    When writing code I think there are a few old axioms:

    • Keep it simple; it will get complicated all on its own – Graham S. lives by this.
    • Fail to plan or Plan to fail
    • Code is better when eating cake.

    Tracking and fixing bugs makes up a big part of a programmers life, even extensive testing can not prove bugs do not exist; as Dijkstra so rightly said ‘Testing shows the presence, not the absence of bugs’

    There already exists some tools, with Visual Studio 2010 greatly enhancing them to track memory leakage, hot paths, bad references and the programmers just being idiots but we still have to live in a world where bugs still exist, regrettably we can’t just put our programs in bio-suits and hope for the best.

    So I was thinking about how you can programmatically track bugs in C#, barring any internal features of Visual Studio or whatever IDE you are using. I wanted a bug tracking experience which was easy, open and as close to the act of programming as possible.

    What I came up with is using a Custom Attribute to Tag Classes, Methods etc. with information about the bug fix. This can be scanned by Reflection to find and output this information.

    Below is the attribute class, this can be placed in either an externally referenced assembly or internally in the project whose bugs you wish to track.

    namespace BugTracker
    {
        using System;
    
        ///
    <summary> /// Custom Attribute class which allows the tracking of bug fixes.
     /// </summary>
        [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
        public class BugAttribute : System.Attribute
        {
            ///
    <summary> /// The Unique identifier for the bug
     /// </summary>
            private string bugID;
    
            ///
    <summary> /// The name of the developer that fixed the bug.
     /// </summary>
            private string fixedBy;
    
            ///
    <summary> /// The description of the bug fix
     /// </summary>
            private string description;
    
            ///
    <summary> /// The datestamp of when the bug was fixed.
     /// </summary>
            private DateTime dateFixed;
    
            ///
    <summary> /// The constructor class for the BugAttribute Type.
     /// </summary>
            ///Unique identifier for the bug
            ///Who fixed the bug
            ///When the bug was fixed
            ///Description of the bug fix
            public BugAttribute(string bugID, string fixedBy, DateTime dateFixed, string description)
            {
                this.bugID = bugID;
                this.fixedBy = fixedBy;
                this.dateFixed = dateFixed;
                this.description = description;
            }
    
            ///
    <summary> /// Gets the unique identifier for the bug.
     /// </summary>
            public string BugID
            {
                get
                {
                    return this.bugID;
                }
            }
    
            ///
    <summary> /// Gets the name of who fixed the bug
     /// </summary>
            public string FixedBy
            {
                get
                {
                    return this.fixedBy;
                }
            }
    
            ///
    <summary> /// Gets the description of the Bug Fix
     /// </summary>
            public string Description
            {
                get
                {
                    return this.description;
                }
            }
    
            ///
    <summary> /// Gets the date and time of when the bug was fixed
     /// </summary>
            public DateTime DateFixed
            {
                get
                {
                    return this.dateFixed;
                }
            }
        }
    }
    

    To use this attribute you just need to put an attribute tag on a method or class E.g.

    [BugAttribute("Bug1", "K. Pfister", new DateTime(2012, 1, 29), "Darn bugs!")]
    private void Example()
    {
    
    }
    

    I am currently working on the associated program to track these attributes, or even if I can build a plugin for Visual Studio (or even Microsoft Word so that bug information can be used to produce reports). To track the Custom Attribute I am using reflection, which I have talked about previously to load in and scan the assemblies which use the attribute.

    To actually read the Custom Attribute you would need to scan through the methods, which can be done easily using the following code snippet.

    
    foreach (Type type in assembly.GetTypes())
    {
        foreach (MethodInfo methodInfo in type.GetMethods())
        {
            MethodBody body = methodInfo.GetMethodBody();
            if (body != null)
            {
                // We now have access to Metadata and MSIL in the Method
            }
        }
    }
    

    Each MethodInfo contains a list of custom attributes which can be scanned through.

    The following example shows how to get the first attribute of Type BugAttribute  from a MethodInfo (method)

    BugAttribute ba = (BugAttribute)method.GetCustomAttributes(typeof(BugAttribute), true)[0];
    

    About

    Software engineer. Tea drinker

    http://MrPfister.com

    Leave a Reply

    Your email address will not be published. Required fields are marked *