0

I'd like to represent containers within containers and have created the following class structure:

public class ContainerLib<T>
{
    /// <summary>
    ///  Generic container type
    /// </summary>
    public T Container { get; set; }

    /// <summary>
    /// Container type
    /// </summary>
    public Type ContainerType {
        get { return ContainerType; }
        set { value = typeof (T); }
    }

    /// <summary>
    /// ContainerLib enum
    /// May change to IList
    /// </summary>
    public IList<ContainerLib<T>> Containers { get; set; }
}

/// <summary>
/// Video container
/// </summary>
public class Container
{
    /// <summary>
    /// Container ID
    /// </summary>
    public int Id { get; set; }


    public string Name { get; set; }

    /// <summary>
    /// Details about the physical location of this container
    /// </summary>
    public LocationDefinition LocationDefinition { get; set; }

    /// <summary>
    /// Type of container - {layout, perspective, source
    /// </summary>
    public string ContainerType { get; set; }

    /// <summary>
    /// List of containers
    /// </summary>
    public IList<ContainerLib<Container>> Containers { get; set; }

}

I'm a little stuck on how to implement this... if I'm even doing this correctly:

        var containerLib = new ContainerLib<Container>
            {
                Container = new Container
                    {
                        Id = 1,
                        LocationDefinition = new LocationDefinition
                            {
                                WidthPixels = 100,
                                HeightPixels = 100,
                                NumColumns = 10,
                                NumRows = 10,
                                TopLeftX = 0,
                                TopLeftY = 0
                            },
                        ContainerType = "Layout"
                    }
            };

        containerLib.Containers = new List<ContainerLib<Container>>
            {
                // build nested structure here ../../../../

            };

So I'd like to set add data to the structure, then iterate through it.

I found an efficient way of using a Dictionary, but that's not what I'm looking for.

I added

public IList<ContainerLib<Container>> Containers { get; set; }

to Container, and by doing that I'm wondering if I even need the ContainerLib<T> anymore?

Assistance appreciated.

4

1 回答 1

3

不确定我是否完全理解了这个问题,但是拥有这样的类不是很简单(为清楚起见,省略了错误检查和私有支持字段),因此您根本不需要额外的 ContainerLib 类:

public class Container
{
   public Container Parent{get;set;} //this is here in case you need to go back up :)
   public List<Container> Children{get;set;}
   //other properties i.e actual data go here

   public Container(Container parent)
   {
     this.Parent=parent;
      Children=new List<Container>();
   }

   public void AddContainer(Container child)
   {
     Children.Add(child);
     child.Parent=this;
   }

}

然后,您可以像这样从上到下反复遍历整个结构:

void WalkContainerTree(Container currentRoot)
{
  if (currentRoot==null) return;
  //process the properties of currentRoot here to do whatever you like with them
  //then do the kids
   foreach (var child in currentRoot.Children)
   {
     WalkContainerTree(child);
   }
}

如果我误读了这个问题,我深表歉意:)

于 2013-10-02T19:17:31.153 回答