共用方式為


IHierarchyData.GetParent 方法

定義

取得 IHierarchyData 物件,表示目前階層式節點的父節點。

public:
 System::Web::UI::IHierarchyData ^ GetParent();
public System.Web.UI.IHierarchyData GetParent ();
abstract member GetParent : unit -> System.Web.UI.IHierarchyData
Public Function GetParent () As IHierarchyData

傳回

IHierarchyData

IHierarchyData 物件,表示目前階層式節點的父節點。

範例

下列程式碼範例示範如何在實作 介面的類別中實 GetParentIHierarchyData 方法。 類別會 FileSystemHierarchyData FileSystemInfo 包裝 物件,而 GetParent 方法實作會檢查 物件的類型 FileSystemInfo ,並根據類型傳回適當的父物件。 此程式碼範例是介面和 HierarchicalDataSourceControl 類別所提供較大範例的 IHierarchyData 一部分。

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

備註

介面 IHierarchyData 未定義 HasParent 便利屬性,因此呼叫端必須檢查 方法 nullGetParent 傳回值,以判斷目前 IHierarchyData 節點是否為節點所屬階層式資料結構的根節點。

適用於

另請參閱