Shortcut To Creating Properties in C-Sharp

I was looking for an easy and consistent way of creating properties for classes.  I sometimes find that it can be a long drawn out process creating 5 to 100 properties for database classes.  Here is a quick example of what I did.
You will need the following references to make this example work.

  1. System.Diagnostics;
  2. System.Reflection;
  3. System.Collections;
public class ErrorLogRecord
{
     Hashtable _hsh = new Hashtable();
     public DateTime DateOfOccurance
     {
          get { return (DateTime?)_hsh[GetName()] ?? DateTime.Now; }
          set { _hsh[GetName()] = value; }
     }        
     public String ErrorText
     {
          get { return (String)_hsh[GetName()]; }
          set { _hsh[GetName()] = value; }
     }

     public String GetName()
     {
          StackTrace stackTrace = new StackTrace();
          StackFrame stackFrame = stackTrace.GetFrame(1);
          MethodBase methodBase = stackFrame.GetMethod();
          return methodBase.Name.Replace("set_", "").Replace("get_", "");        
     }
}

While this method is probably not perfect and could be argued there is a better way, this seems to satisfy the need. I would welcome alternatives.

Alternate Reading: Shortcut To Creating Properties in C-Sharp (revisited)

Posted in Better Coding, C#, Properties
4 comments on “Shortcut To Creating Properties in C-Sharp
  1. Sekhat says:

    What’s wrong with

    public class SomeClass
    {
    public string SomeStringProperty { get; set; }
    }

    The Compiler automatically creates the backing field for you and you get fully working property.

    Plus you can still do

    public class SomeClass
    {
    public string SomeStringProperty { get; private set; }
    }

    Which makes the setter only visible to the internals of the class 🙂

    • There is nothing wrong with it. It is a great shortcut, which I have used for simple classes.

      However; I find that there are many times that I need a dictionary, hashtable or other to store the properties for further manipulation. As an example it there are plenty of examples how to turn a dictionary or hastable into an array that can be passed into a DataRow or other objects with a single line of code. Using a single line of code to take my properties and put them into an array and then into DataRow or other is very appealing to me.

      I had met another developer that has a similar approach to mine and had the same outcome, but it was slightly different again. When you are presented with a wall, you find ways to get over it and this was my way and that doesn’t mean it is the only way. I always have my ear to the ground for other solutions.

      • Sekhat says:

        I think it just seems a bit of a faff to me, I would probably write my classes normally, and if I really need to turn the class into a dictionary or the like I would probably write an extension method to convert it to one. Probably a generic extension method that uses reflection to get the values and property names out. Saves all the casting, reflection name lookups and boxing until it’s needed.

        Something like: https://gist.github.com/1264422

        Of course, that doesn’t make what I do any more right than yours, and I’ll keep you solution in mind in case I come across something in my travels that will benefit from it.

        • Thanks for the Link to your example code. I do like that.

          I don’t believe anyone is every completely right as there are many ways for doing the same thing. Sometimes it is a matter of opinion. I am going to convert one of my classes to do what you are suggesting and see what happens.

          One of the things I like to do is benchmark tests to see how things look in memory usage, CPU and overall speed. I will find this to be an interesting experiment.

          This is a great example of collaborating using social media and blogs. Thanks for sharing.

Leave a Reply

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

*