IHierarchyData Interface

Definition

Exposes a node of a hierarchical data structure, including the node object and some properties that describe characteristics of the node. Objects that implement the IHierarchyData interface can be contained in IHierarchicalEnumerable collections, and are used by ASP.NET site navigation and data source controls.

C#
public interface IHierarchyData
Derived

Examples

The following code example demonstrates how to implement the IHierarchyData interface with a class that wraps a FileSystemInfo object. The FileSystemInfo class is a good example of a hierarchical data node, which the IHierarchyData interface represents for ASP.NET hierarchical data source controls. This code example is part of a larger example provided for the HierarchicalDataSourceControl class.

C#
public class FileSystemHierarchyData : IHierarchyData
{
    private FileSystemInfo fileSystemObject = null;

    public FileSystemHierarchyData(FileSystemInfo obj)
    {
        fileSystemObject = obj;
    }

    public override string ToString()
    {
        return fileSystemObject.Name;
    }
    // IHierarchyData implementation.
    public bool HasChildren
    {
        get
        {
            if (typeof(DirectoryInfo) == fileSystemObject.GetType())
            {
                DirectoryInfo temp = (DirectoryInfo)fileSystemObject;
                return (temp.GetFileSystemInfos().Length > 0);
            }
            else
            {
                return false;
            }
        }
    }
    // DirectoryInfo returns the OriginalPath, while FileInfo returns
    // a fully qualified path.
    public string Path
    {
        get
        {
            return fileSystemObject.ToString();
        }
    }
    public object Item
    {
        get
        {
            return fileSystemObject;
        }
    }
    public string Type
    {
        get
        {
            return "FileSystemData";
        }
    }
    public IHierarchicalEnumerable GetChildren()
    {
        FileSystemHierarchicalEnumerable children =
            new FileSystemHierarchicalEnumerable();

        if (typeof(DirectoryInfo) == fileSystemObject.GetType())
        {
            DirectoryInfo temp = (DirectoryInfo)fileSystemObject;
            foreach (FileSystemInfo fsi in temp.GetFileSystemInfos())
            {
                children.Add(new FileSystemHierarchyData(fsi));
            }
        }
        return children;
    }

    public IHierarchyData GetParent()
    {
        FileSystemHierarchicalEnumerable parentContainer =
            new FileSystemHierarchicalEnumerable();

        if (typeof(DirectoryInfo) == fileSystemObject.GetType())
        {
            DirectoryInfo temp = (DirectoryInfo)fileSystemObject;
            return new FileSystemHierarchyData(temp.Parent);
        }
        else if (typeof(FileInfo) == fileSystemObject.GetType())
        {
            FileInfo temp = (FileInfo)fileSystemObject;
            return new FileSystemHierarchyData(temp.Directory);
        }
        // If FileSystemObj is any other kind of FileSystemInfo, ignore it.
        return null;
    }
}

The following code example demonstrates how to recursively iterate through an IHierarchicalEnumerable collection, extract the IHierarchyData item from the enumerator using the GetHierarchyData method, and perform basic work with the data item.

ASP.NET (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ihd_1.aspx.cs" Inherits="ihd_1_aspx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Remarks

The IHierarchyData interface is implemented by classes that represent nodes of a hierarchical structure, and track the hierarchical relationships to their parent and child nodes. Classes that implement the IHierarchyData interface can be contained in collections that implement the IHierarchicalEnumerable interface.

Properties

HasChildren

Indicates whether the hierarchical data node that the IHierarchyData object represents has any child nodes.

Item

Gets the hierarchical data node that the IHierarchyData object represents.

Path

Gets the hierarchical path of the node.

Type

Gets the name of the type of Object contained in the Item property.

Methods

GetChildren()

Gets an enumeration object that represents all the child nodes of the current hierarchical node.

GetParent()

Gets an IHierarchyData object that represents the parent node of the current hierarchical node.

Applies to

Proizvod Verzije
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also