Click here to Skip to main content
12,243,522 members (44,984 online)
Click here to Skip to main content
Articles » Languages » C# » Enumerations » Downloads

Stats

43.6K views
181 downloads
14 bookmarked
Posted

Tree Structured Enumerations

, 2 Apr 2008 CPOL
The way to maintain a tree structured enumeration while having all the advantages of the standard ones
using System;
using System.Collections.Generic;

namespace TreeStructuredEnumerations
{
    public class EnumUtility
    {
        public static bool IsChild<TType>(TType child, TType parent)
        {
            Type enumType = typeof(TType);
            Object[] attributeList = enumType.GetCustomAttributes(typeof(StructuredAttribute), true);

            if (attributeList.Length > 0)
            {
                StructuredAttribute attribute = (StructuredAttribute)attributeList[0];
                int span = attribute.Span;
                int parentIndex = (int)Convert.ChangeType(parent, typeof(int));
                int childIndex = (int)Convert.ChangeType(child, typeof(int));
                int index = childIndex / span;
                return (index == parentIndex && childIndex != parentIndex);
            }

            return true;
        }

        public static List<TType> CreateList<TType>(TType parent)
        {
            List<TType> result = new List<TType>();
            Type enumType = typeof (TType);
            TType[] enumValues = (TType[]) Enum.GetValues(enumType);

            foreach (TType enumValue in enumValues)
            {
                if (IsChild(enumValue, parent))
                {
                    result.Add(enumValue);
                }
            }

            return result;
        }
    }
}

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)

Share

About the Author

Smart K8
Software Developer
Czech Republic Czech Republic
Contacts: EMAIL - smartk8@gmail.com

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 2 Apr 2008
Article Copyright 2008 by Smart K8
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid