示例#1
0
    private void BindObject(object obj, TreeNode root)
    {
        int index = 0;

        if (obj is DataRow)
        {
            // DataRow source, bind column names
            DataRow dr = (DataRow)obj;

            // Create tree structure
            foreach (DataColumn col in dr.Table.Columns)
            {
                // Stop on max nodes
                if (index++ >= MacroResolver.MaxMacroNodes)
                {
                    AppendMore(root);
                    break;
                }

                // Add the column
                object childObj = dr[col.ColumnName];
                AppendChild(root, col.ColumnName, childObj, false);
            }
        }
        else if (obj is DataRowView)
        {
            // DataRowView source, bind column names
            DataRowView dr = (DataRowView)obj;

            // Create tree structure
            foreach (DataColumn col in dr.DataView.Table.Columns)
            {
                // Stop on max nodes
                if (index++ >= MacroResolver.MaxMacroNodes)
                {
                    AppendMore(root);
                    break;
                }

                // Add the column
                object childObj = dr[col.ColumnName];
                AppendChild(root, col.ColumnName, childObj, false);
            }
        }
        else if (obj is AbstractContext)
        {
            AbstractContext context = (AbstractContext)obj;

            // Create tree structure
            foreach (string col in context.Properties)
            {
                // Stop on max nodes
                if (index >= MacroResolver.MaxMacroNodes)
                {
                    AppendMore(root);
                    break;
                }

                // Add the property
                object childObj = context.GetProperty(col);
                AppendChild(root, col, childObj, false);
            }
        }
        else if (obj is IHierarchicalObject)
        {
            // Data container source
            IHierarchicalObject hc = (IHierarchicalObject)obj;

            // Create tree structure
            foreach (string col in hc.Properties)
            {
                // Stop on max nodes
                if (index++ >= MacroResolver.MaxMacroNodes)
                {
                    AppendMore(root);
                    break;
                }

                // Add the property
                object childObj = null;
                try
                {
                    hc.TryGetProperty(col, out childObj);
                }
                catch
                {
                }

                AppendChild(root, col, childObj, false);
            }
        }
        else if (obj is IDataContainer)
        {
            // Data container source
            IDataContainer dc = (IDataContainer)obj;

            // Create tree structure
            foreach (string col in dc.ColumnNames)
            {
                // Stop on max nodes
                if (index++ >= MacroResolver.MaxMacroNodes)
                {
                    AppendMore(root);
                    break;
                }

                // Add the column
                object childObj = null;
                dc.TryGetValue(col, out childObj);

                AppendChild(root, col, childObj, false);
            }
        }

        // Enumerable objects
        if ((obj is IEnumerable) && !(obj is string))
        {
            IEnumerable collection = (IEnumerable)obj;
            IEnumerator enumerator = null;

            bool indexByName = false;

            INamedEnumerable namedCol = null;
            if (obj is INamedEnumerable)
            {
                // Collection with name enumerator
                namedCol = (INamedEnumerable)collection;
                if (namedCol.ItemsHaveNames)
                {
                    enumerator  = namedCol.GetNamedEnumerator();
                    indexByName = true;
                }
            }

            if (!indexByName)
            {
                // Standard collection
                enumerator = collection.GetEnumerator();
            }

            int i = 0;

            while (enumerator.MoveNext())
            {
                // Stop on max nodes
                if (index++ >= MacroResolver.MaxMacroNodes)
                {
                    AppendMore(root);
                    break;
                }

                // Add the item
                object item = SqlHelperClass.EncapsulateObject(enumerator.Current);
                if (indexByName)
                {
                    // Convert the name with dot to indexer
                    string name = namedCol.GetObjectName(item);
                    if (!ValidationHelper.IsIdentifier(name))
                    {
                        name = "[\"" + name + "\"]";
                    }

                    AppendChild(root, name, item, false);
                }
                else
                {
                    // Indexed item
                    AppendChild(root, i.ToString(), item, true);
                }

                i++;
            }
        }
    }