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 对象,该对象表示当前分层节点的父节点。

示例

下面的代码示例演示如何在实现接口的IHierarchyData类中实现GetParent方法。 类 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 便利属性,因此调用方必须检查方法的 GetParent 返回值,以确定 null 当前 IHierarchyData 节点是否是节点所属的分层数据结构的根节点。

适用于

另请参阅