Used when DataBinding internally by EntitySpaces
Inheritance: System.ComponentModel.PropertyDescriptor
        internal override PropertyDescriptorCollection GetProperties(tgEntity entity, tgEntityCollectionBase baseCollection)
        {
            bool weHaveData  = false;
            int  lastOrdinal = 0;

            tgColumnMetadataCollection esMetaCols = entity.tg.Meta.Columns;

            tgEntityCollectionBase theBaseCollection = baseCollection != null ? baseCollection : entity.Collection;

            bool enableHierarchcialBinding = theBaseCollection != null ? theBaseCollection.EnableHierarchicalBinding : true;

            if (theBaseCollection != null)
            {
                if (theBaseCollection.GetList() != null)
                {
                    // Do we have any entities?
                    weHaveData = theBaseCollection.GetList().Count > 0;

                    if (weHaveData == false)
                    {
                        // If selectedColumns has data then they attempted a load and we know the columns based on thier select statement
                        weHaveData = theBaseCollection.selectedColumns != null && theBaseCollection.selectedColumns.Keys.Count > 0;
                    }
                }
            }

            //------------------------------------------------------------
            // First we deal with Properties from the DataTable.Columns
            // or from the tgColumnMetadataCollection.
            //------------------------------------------------------------
            ArrayList collNested = new ArrayList();
            SortedList <int, PropertyDescriptor> coll = new SortedList <int, PropertyDescriptor>();

            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(entity, true);

            // Note, we check for selectedColumns because we might be a deserialized collection in which
            // case there will not be any selectedColumns
            if (weHaveData && theBaseCollection.selectedColumns != null)
            {
                SortedList list = GetSortedListOfProperties(entity, baseCollection);

                for (int i = 0; i < list.Count; i++)
                {
                    string column = (string)list.GetByIndex(i);

                    if (column == "ESRN")
                    {
                        continue;
                    }

                    tgColumnMetadata esCol = entity.tg.Meta.Columns.FindByColumnName(column);

                    if (esCol != null)
                    {
                        PropertyDescriptor prop = props[esCol.PropertyName];

                        if (prop != null)
                        {
                            coll.Add(lastOrdinal++, prop);
                        }
                    }
                    else
                    {
                        esCol = theBaseCollection.extraColumnMetadata[column];

                        if (esCol != null)
                        {
                            // Extra or Extended Properties
                            tgPropertyDescriptor dpd = new tgPropertyDescriptor
                                                       (
                                typeof(T),
                                column,
                                esCol != null ? esCol.Type : typeof(string),
                                delegate(object p)
                            {
                                return(((tgEntity)p).currentValues[column]);
                            },
                                delegate(object p, object data)
                            {
                                ((tgEntity)p).currentValues[column] = data;
                                ((tgEntity)p).OnPropertyChanged(column);
                            }
                                                       );

                            coll.Add(lastOrdinal++, dpd);
                        }
                    }
                }
            }
            else
            {
                foreach (tgColumnMetadata esCol in esMetaCols)
                {
                    coll.Add(lastOrdinal++, props[esCol.PropertyName]);
                }
            }

            //------------------------------------------------------------
            // Now we deal with extended properties that are using the
            // esExtendedPropertyAttribute technique
            //------------------------------------------------------------
            foreach (PropertyDescriptor prop in props)
            {
                if (prop.Attributes.Contains(tgEntityCollection <T> .extendedPropertyAttribute))
                {
                    coll.Add(lastOrdinal++, prop);
                }
            }

            //------------------------------------------------------------
            // Now we deal with any local properties. Local properties are
            // properties that users may want to bind with that are
            // NOT backed by data in the DataTable
            //------------------------------------------------------------
            List <tgPropertyDescriptor> localProps = entity.GetLocalBindingProperties();

            if (localProps != null)
            {
                foreach (tgPropertyDescriptor esProp in localProps)
                {
                    // We check this incase they add a local based property for a DataColumn
                    // based property, they would do this so it appears in design time, and
                    // we don't want to add a duplicate
                    bool exists = coll.ContainsValue(props[esProp.Name]);

                    if (!exists)
                    {
                        if (props[esProp.Name] != null)
                        {
                            coll.Add(lastOrdinal++, props[esProp.Name]);
                        }
                        else
                        {
                            coll.Add(lastOrdinal++, esProp);
                        }
                    }
                }
            }

            ArrayList tempColl = new ArrayList();

            if (enableHierarchcialBinding)
            {
                List <tgPropertyDescriptor> hierProps = entity.GetHierarchicalProperties();
                if (hierProps != null)
                {
                    foreach (tgPropertyDescriptor esProp in hierProps)
                    {
                        esProp.TrueDescriptor = props[esProp.Name];
                        //  coll.Add(lastOrdinal++, esProp);

                        tempColl.Add(esProp);
                    }
                }
            }

            // Create the collection
            foreach (PropertyDescriptor p in coll.Values)
            {
                tempColl.Add(p);
            }
            tempColl.AddRange(collNested);

            PropertyDescriptorCollection theProps =
                new PropertyDescriptorCollection((PropertyDescriptor[])tempColl.ToArray(typeof(PropertyDescriptor)));

            return(theProps);
        }
        PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors)
        {
            // We are binding so we turn this on ...
            entities.RaiseListChangedEvents = true;

            PropertyDescriptorCollection props = null;
            Type type = null;

            if (listAccessors == null || listAccessors.Length == 0)
            {
                tgEntity e = null;

                // It wants "this" object and so we can use this technique
                if (this.Count > 0)
                {
                    e = entities[0] as tgEntity;
                }
                else
                {
                    e = new T();
                }

                type = e.GetType();

                //------------------------------------
                // Check to see if it's in the cached
                //------------------------------------
                if (this.BindingPropertyCache.ContainsKey(type))
                {
                    return(this.BindingPropertyCache[type]);
                }

                props = this.GetProperties(e, this);

                this.BindingPropertyCache[type] = props;
            }
            else
            {
                if (this.EnableHierarchicalBinding == false)
                {
                    return(null);
                }

                // We should not enter this else statement if hierarchical binding is false
                PropertyDescriptor prop = listAccessors[listAccessors.Length - 1];

                //------------------------------------
                // Check to see if it's in the cached
                //------------------------------------
                if (this.BindingPropertyCache.ContainsKey(prop.PropertyType))
                {
                    return(this.BindingPropertyCache[prop.PropertyType]);
                }

                tgPropertyDescriptor esProp = prop as tgPropertyDescriptor;

                if (esProp != null)
                {
                    // Nope, not in the cache, let's get the info
                    props = this.GetProperties(esProp.ContainedEntity, null);
                }
                else
                {
                    // I give up, go get the raw properties
                    props = TypeDescriptor.GetProperties(prop.PropertyType);
                }

                this.BindingPropertyCache[prop.PropertyType] = props;
            }

            return(props);
        }