Skip to main content
Email Password   helpLost your password?

Introduction

C# 3.0 introduces a great new feature called Automatic Properties, and if you haven’t already read about them, I would encourage you to read Scott Guthrie's introductory post.

As great as they are and as much time as they save, Automatic Properties have a serious drawback – you can’t set the default value of the property. Instead, the compiler will initialize value properties to 0, reference properties to null, and enum’s to the first member, and while this might work for some applications, it wasn’t working for mine.

Background

When I thought about the implementation, two options became apparent. One, I could create a base object class and have all of my classes inherit from this base class. This however isn’t a great solution because a number of my classes inherit from other classes outside of my control, and since .NET does not support multiple inheritances, it was clear this wasn’t going to work. To my rescue was the also new C# 3.0 feature, Extension Methods. If you haven’t already heard about Extension Methods, I’d recommend reading another one of Scott Guthrie's blog posts about them.

Using the Code

Using the code requires that you decorate your properties with an attribute already available in the System.ComponentModel namespace – if you haven’t already guessed it, it’s the aptly named DefaultValueAttribute attribute. As well, it requires a quick call to the InitDefaults() extension method from the constructor which I will discuss a bit later.

The attached code supplies a demo implementation of the TestObject and TestObjectInherited classes:

public class TestObject
    {
        public TestObject()
        {
            this.InitDefaults();
        }

        [DefaultValue(-45)]
        public int DefaultInt
        {
            get;
            set;
        }

        [DefaultValue(10.23)]
        public double DefaultDouble
        {
            get;
            set;
        }

        [DefaultValue(true)]
        public bool DefaultBool
        {
            get;
            set;
        }

        [DefaultValue(TestEnum.Value2)]
        public TestEnum DefaultEnum
        {
            get;
            set;
        }

        [DefaultValue("DefaultString!")]
        public string DefaultString
        {
            get;
            set;
        }

        public string StringWithoutDefault
        {
            get;
            set;
        }

        public string ValueOfPrivateProperty
        {
            get
            {
                return PrivateProperty;
            }
        }

        [DefaultValue("This is a private property!")]
        protected string PrivateProperty
        {
            get;
            set;        
        }
    }

The magical InitDefaults() method is implemented as an extension method which uses reflection to set the value of the properties to the default value:

public static void InitDefaults(this object o)
        {
            PropertyInfo[] props = o.GetType().GetProperties(BindingFlags.Public | 
                BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);

            for (int i = 0; i < props.Length; i++)
            {
                PropertyInfo prop = props[i];                

                if (prop.GetCustomAttributes(true).Length > 0)
                {
                    object[] defaultValueAttribute = 
                        prop.GetCustomAttributes(typeof(DefaultValueAttribute), true);

                    if (defaultValueAttribute != null)                    
                    {
                        DefaultValueAttribute dva = 
                            defaultValueAttribute[0] as DefaultValueAttribute;
                        
                        if(dva != null)
                            prop.SetValue(o, dva.Value, null);                                
                    }
                }
            }
        }

Points of Interest

I decided to support initializing the default value of properties in inherited classes, but if you don’t want this behavior, you can simply pass false to GetCustomAttributes().

if (prop.GetCustomAttributes(false).Length > 0)
                {
                    object[] defaultValueAttribute = prop.GetCustomAttributes
                        (typeof(DefaultValueAttribute), false);

                    if (defaultValueAttribute != null)                    
                    {
                        DefaultValueAttribute dva = 
                            defaultValueAttribute[0] as DefaultValueAttribute;
                        
                        if(dva != null)
                            prop.SetValue(o, dva.Value, null);                                
                    }
                }

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalover-design? Pin
ffffffffffffffffffff
10:03 29 Oct '08  
GeneralDiffent approach Pin
Günther M. FOIDL
1:01 8 Oct '08  
RantVote for initialization to be added as a language feature. Pin
Laughing.John
14:08 23 Aug '08  
GeneralI don't think its a good idea Pin
Jorge Bay Gondra
4:18 22 Aug '08  
GeneralAnother option Pin
Ajek
12:02 20 May '08  
GeneralRe: Another option Pin
nsimeonov
17:32 9 Jun '09  
GeneralA opinion Pin
Ricky Wang
20:57 19 May '08  
GeneralI'd do it differently... Pin
PIEBALDconsult
14:49 14 May '08  
GeneralRe: I'd do it differently... Pin
Seishin#
2:56 15 May '08  
GeneralRe: I'd do it differently... Pin
PIEBALDconsult
6:56 15 May '08  
GeneralRe: I'd do it differently... Pin
PIEBALDconsult
8:39 15 May '08  
GeneralRe: I'd do it differently... Pin
Seishin#
9:30 15 May '08  
GeneralRe: I'd do it differently... Pin
PIEBALDconsult
10:45 15 May '08  
GeneralRe: I'd do it differently... Pin
Seishin#
10:47 15 May '08  
GeneralRe: I'd do it differently... Pin
PIEBALDconsult
11:15 15 May '08  
GeneralI Don't See The Value In Automatic Properties Pin
John Simmons / outlaw programmer
12:26 14 May '08  
GeneralRe: I Don't See The Value In Automatic Properties [modified] Pin
PIEBALDconsult
14:16 14 May '08  
GeneralRe: I Don't See The Value In Automatic Properties Pin
William E. Kempf
7:47 15 May '08  
GeneralRe: I Don't See The Value In Automatic Properties Pin
PIEBALDconsult
8:53 15 May '08  
GeneralRe: I Don't See The Value In Automatic Properties Pin
William E. Kempf
9:37 15 May '08  
GeneralRe: I Don't See The Value In Automatic Properties Pin
PIEBALDconsult
10:29 15 May '08  
GeneralRe: I Don't See The Value In Automatic Properties Pin
William E. Kempf
10:42 15 May '08  
GeneralRe: I Don't See The Value In Automatic Properties Pin
Steven Berkovitz
10:44 15 May '08  
GeneralRe: I Don't See The Value In Automatic Properties Pin
William E. Kempf
10:46 15 May '08  
GeneralRe: I Don't See The Value In Automatic Properties Pin
PIEBALDconsult
10:49 15 May '08  


Last Updated 14 May 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009