IHierarchyData インターフェイス

定義

ノード オブジェクトやノードの特性を記述するいくつかのプロパティを含む、階層データ構造のノードを公開します。 IHierarchyData インターフェイスを実装するオブジェクトは、IHierarchicalEnumerable コレクションに含めることができます。また、ASP.NET サイト ナビゲーションおよびデータ ソース コントロールで使用されます。

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

次のコード例では、IHierarchyData オブジェクトをラップするクラスを使用してFileSystemInfo インターフェイスを実装する方法を示します。 FileSystemInfo クラスは、IHierarchyData インターフェイスが ASP.NET 階層データ ソース コントロール用に表す階層データ ノードの良い例です。 このコード例は、 HierarchicalDataSourceControl クラスに提供されるより大きな例の一部です。

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 コレクションを再帰的に反復処理し、IHierarchyData メソッドを使用して列挙子からGetHierarchyData項目を抽出し、データ項目の基本的な処理を実行する方法を示します。

<%@ 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 インターフェイスは、階層構造のノードを表し、親ノードと子ノードとの階層関係を追跡するクラスによって実装されます。 IHierarchyData インターフェイスを実装するクラスは、IHierarchicalEnumerable インターフェイスを実装するコレクションに含めることができます。

プロパティ

名前 説明
HasChildren

IHierarchyData オブジェクトが表す階層データ ノードに子ノードがあるかどうかを示します。

Item

IHierarchyData オブジェクトが表す階層データ ノードを取得します。

Path

ノードの階層パスを取得します。

Type

Object プロパティに含まれるItemの型の名前を取得します。

メソッド

名前 説明
GetChildren()

現在の階層ノードのすべての子ノードを表す列挙オブジェクトを取得します。

GetParent()

現在の階層ノードの親ノードを表す IHierarchyData オブジェクトを取得します。

適用対象

こちらもご覧ください