IHierarchyData 接口

定义

公开分层数据结构的节点,包括节点对象和描述节点特征的一些属性。Exposes a node of a hierarchical data structure, including the node object and some properties that describe characteristics of the node. 实现 IHierarchyData 接口的对象可以包含在 IHierarchicalEnumerable 集合中,并由 ASP.NET 站点导航和数据源控件所使用。Objects that implement the IHierarchyData interface can be contained in IHierarchicalEnumerable collections, and are used by ASP.NET site navigation and data source controls.

public interface class IHierarchyData
public interface IHierarchyData
type IHierarchyData = interface
Public Interface IHierarchyData
派生

示例

下面的代码示例演示如何使用IHierarchyData FileSystemInfo包装对象的类实现接口。The following code example demonstrates how to implement the IHierarchyData interface with a class that wraps a FileSystemInfo object. 类是层次结构数据节点的一个很好的示例, IHierarchyData接口表示该节点的 ASP.NET 分层数据源控件。 FileSystemInfoThe FileSystemInfo class is a good example of a hierarchical data node, which the IHierarchyData interface represents for ASP.NET hierarchical data source controls. 此代码示例是为HierarchicalDataSourceControl类提供的更大示例的一部分。This code example is part of a larger example provided for the HierarchicalDataSourceControl class.

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;
    }
}

Public Class FileSystemHierarchyData
    Implements IHierarchyData

    Public Sub New(ByVal obj As FileSystemInfo)
        fileSystemObject = obj
    End Sub

    Private fileSystemObject As FileSystemInfo = Nothing

    Public Overrides Function ToString() As String
        Return fileSystemObject.Name
    End Function

    ' IHierarchyData implementation.
    Public Overridable ReadOnly Property HasChildren() As Boolean _
     Implements IHierarchyData.HasChildren
        Get
            If GetType(DirectoryInfo) Is fileSystemObject.GetType() Then
                Dim temp As DirectoryInfo = _
                    CType(fileSystemObject, DirectoryInfo)
                Return temp.GetFileSystemInfos().Length > 0
            Else
                Return False
            End If
        End Get
    End Property
    ' DirectoryInfo returns the OriginalPath, while FileInfo returns
    ' a fully qualified path.

    Public Overridable ReadOnly Property Path() As String _
     Implements IHierarchyData.Path
        Get
            Return fileSystemObject.ToString()
        End Get
    End Property

    Public Overridable ReadOnly Property Item() As Object _
     Implements IHierarchyData.Item
        Get
            Return fileSystemObject
        End Get
    End Property

    Public Overridable ReadOnly Property Type() As String _
     Implements IHierarchyData.Type
        Get
            Return "FileSystemData"
        End Get
    End Property

    Public Overridable Function GetChildren() _
        As IHierarchicalEnumerable _
        Implements IHierarchyData.GetChildren

        Dim children As New FileSystemHierarchicalEnumerable()

        If GetType(DirectoryInfo) Is fileSystemObject.GetType() Then
            Dim temp As DirectoryInfo = _
                CType(fileSystemObject, DirectoryInfo)
            Dim fsi As FileSystemInfo
            For Each fsi In temp.GetFileSystemInfos()
                children.Add(New FileSystemHierarchyData(fsi))
            Next fsi
        End If
        Return children
    End Function 'GetChildren


    Public Overridable Function GetParent() As IHierarchyData _
     Implements IHierarchyData.GetParent
        Dim parentContainer As New FileSystemHierarchicalEnumerable()

        If GetType(DirectoryInfo) Is fileSystemObject.GetType() Then
            Dim temp As DirectoryInfo = _
                CType(fileSystemObject, DirectoryInfo)
            Return New FileSystemHierarchyData(temp.Parent)
        ElseIf GetType(FileInfo) Is fileSystemObject.GetType() Then
            Dim temp As FileInfo = CType(fileSystemObject, FileInfo)
            Return New FileSystemHierarchyData(temp.Directory)
        End If
        ' If FileSystemObj is any other kind of FileSystemInfo, ignore it.
        Return Nothing
    End Function 'GetParent
End Class

下面的代码示例演示如何以递归方式循环访问IHierarchicalEnumerable集合, 使用GetHierarchyData方法IHierarchyData从枚举器提取项, 并使用数据项执行基本操作。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.

<%@ 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>
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="ihd_1.aspx.vb" 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>

注解

IHierarchyData接口由表示层次结构节点的类实现, 并跟踪其父节点和子节点的层次结构关系。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. 实现IHierarchyData接口的类可以包含在IHierarchicalEnumerable实现接口的集合中。Classes that implement the IHierarchyData interface can be contained in collections that implement the IHierarchicalEnumerable interface.

属性

HasChildren

指示 IHierarchyData 对象所表示的分层数据节点是否有子节点。Indicates whether the hierarchical data node that the IHierarchyData object represents has any child nodes.

Item

获取 IHierarchyData 对象所表示的分层数据节点。Gets the hierarchical data node that the IHierarchyData object represents.

Path

获取节点的分层路径。Gets the hierarchical path of the node.

Type

获取包含在 Item 属性中的 Object 的类型名称。Gets the name of the type of Object contained in the Item property.

方法

GetChildren()

获取一个枚举对象,该对象表示当前分层节点的所有子节点。Gets an enumeration object that represents all the child nodes of the current hierarchical node.

GetParent()

获取 IHierarchyData 对象,该对象表示当前分层节点的父节点。Gets an IHierarchyData object that represents the parent node of the current hierarchical node.

适用于

另请参阅