Click here to Skip to main content
15,898,134 members
Articles / Programming Languages / C#

Key-Value Pairs as Enum-Constants

Rate me:
Please Sign up or sign in to vote.
4.33/5 (7 votes)
7 Sep 2008CPOL3 min read 62.1K   213   19  
An enum-like class that supports flags (up to 8192), has additional value-type data, description, and FastSerializer support.
using System;
using System.Collections.Generic;
using System.Text;

namespace Dialogik.Utils.Defines
{
    public class Instancer
    {
        #region Members

        private CreateHandler   _createHandler;
        #endregion

        #region Constructors

        public Instancer(string hashKey, Type cType)
        {
            _hashKey = hashKey;
            RegisterType(cType, false);
        }

        public Instancer(string hashKey, Type cType, bool listType)
        {
            _hashKey = hashKey;
            RegisterType(cType, listType);
        }
 
        #endregion

        #region Property - HashKey

        private string _hashKey;

        public string HashKey
        {
            get { return _hashKey; }
            set { _hashKey = value; }
        }
        #endregion

        #region Property - ClassType

        private Type _classType;

        public Type ClassType
        {
            get { return _classType; }
            set { _classType = value; }
        }
        #endregion

        #region Method - RegisterType

        private void RegisterType(Type cType, bool buildListType)
        {
            if (buildListType)
            {
                //  we have to build generic ourselves
                Type listBase = typeof(List<>);
                Type listType = listBase.MakeGenericType(cType);
                _classType = listType;
            }
            else
            {
                _classType = cType;
            }
            _createHandler = MethodCompiler.CreateNewHandler(_classType);
        }
        #endregion

        #region Method - CreateNew

        public object CreateNew()
        {
            object _object = null;
            try
            {
                if (_createHandler != null)
                {
                    _object = _createHandler();
                }
                else if (_classType != null)
                {
                    _object = Activator.CreateInstance(_classType);
                }
            }
            catch { }

            return _object;
        }
        #endregion

    };
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions