New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

ClassDescriptor

61 Views
Copy Code Show/Hide Line Numbers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using LinqExtender.Configuration.Serialization;
 
namespace LinqExtender.Configuration
{
    /// <summary>
    /// Contains details for the class.
    /// </summary>
    public class ClassDescriptor
    {
        /// <summary>
        /// Gets/Sets key for the class, normally contains the name of the class.
        /// </summary>
        public string Key { get; set; }
        
        /// <summary>
        /// Gets/ Sets the name of the class.
        /// </summary>
        public string Name { get; set; }
 
        /// <summary>
        /// Sets the info for each property.
        /// </summary>
        internal IDictionary<string, PropertyDescriptor> PropertyDescriptors
        {
            get
            {
                if (propertyDescriptors == null)
                    propertyDescriptors = new Dictionary<string, PropertyDescriptor>();
                return propertyDescriptors;
            }
        }
 
        private IDictionary<string, PropertyDescriptor> propertyDescriptors;
 
        /// <summary>
        /// Builds and returns the xml element for the class.
        /// </summary>
        /// <param name="descriptor"></param>
        /// <returns></returns>
        public static implicit  operator ClassElement(ClassDescriptor descriptor)
        {
            ClassElement element = new ClassElement() { Name = descriptor.Key, EntityName = descriptor.Name };
 
            // creates the Properties collection.
            element.Properties = new PropertyElement[descriptor.PropertyDescriptors.Count];
 
            int index = 0;
 
            foreach (string key in descriptor.PropertyDescriptors.Keys)
            {
                PropertyDescriptor propertyDescriptor = descriptor.PropertyDescriptors[key];
 
                PropertyElement propertyElement = new PropertyElement();
 
                propertyElement.Name = propertyDescriptor.Name;
 
                foreach (string propName in propertyDescriptor.Properties.Keys)
                {
                    PropertyInfo info = typeof (PropertyElement).GetProperty(propName);
 
                    if (info.CanWrite)
                    {
                        info.SetValue(propertyElement, propertyDescriptor.Properties[propName], null);
                    }
                }
                element.Properties[index++] = propertyElement;
            }
 
            return element;
        }
    }
}
by Athens Springer
  December 17, 2009 @ 10:14pm
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate