1

基类 ,VideoContainer包含 的列表VideoContainers

VideoContainer 中的属性与其他三个类共有,其中有三个:

  • Layout
  • Perspective
  • Source

这些类中的每一个都有不同的属性,并且应该适合VideoContainers集合。

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

    /// <summary>
    /// Type of container - {Layout, Perspective, SourceContainer}
    /// 
    /// This is usually set by the instantiated class.
    /// </summary>
    public ContainerTypes ContainerType { get; set; }

    /// <summary>
    /// Parent ID
    /// </summary>
    public VideoContainerIdentifier ParentObject { get; set; }

    /// <summary>
    /// Name of container
    /// </summary>
    public string Name { get; set; }

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

    /// <summary>
    /// When container has a tile applied - number of rows of containers within this perspective
    /// </summary>
    public short NumRows { get; set; }

    /// <summary>
    /// When container has a tile aplpied - the number of columns of containers within this perspective
    /// </summary>
    public short NumColumns { get; set; }


    /// <summary>
    /// List of containers
    /// </summary>
    public IList<VideoContainer<T>> VideoContainers { get; set; }


    /// <summary>
    /// Draw
    /// </summary>
    public virtual void Draw()
    {
        // drawing tasks
    }

}

最初的问题是我无法将 Layout(或其他类类型)放入 VideoContainers 集合中,因为它需要一种 VideoContainer。

所以我补充说<T>,希望但在访问类型的属性时遇到问题<T>- 这是行不通的。

如何正确设置?

- 更新 -

我忘了提到的是类都继承了VideoContainers

根据下面的建议,我创建了public interface IVideoContainer<T>.

Layout 类现在被定义为 public class Layout : IVideoContainer<Layout>并实现了接口的所有方法:

public class Layout : IVideoContainer<Layout>
{
    /// <summary>
    /// ctor
    /// </summary>
    public Layout()
    {
        ContainerType = ContainerTypes.Layout;
    }

    public int Id
    {...

问题在于实施:

        var layout = new IVideoContainer<Layout>
            {
                Id = 1,
                ParentObject = null,
                Name = "Layout Definition 1",
                LocationDefinition = new LocationDefinition
                    {
                        TopLeftX = 0,
                        TopLeftY = 0,
                        WidthPixels = 1000,
                        HeightPixels = 1000
                    },
                NumRows = 20,
                NumColumns = 20,
                VideoContainers = new List<Perspective>
                    {
                        new IVideoContainer<Perspective>
                        {
                            Id = 10, ...

-- 更新 2 --

我现在有:

/// <summary>
/// VideoContainer
/// </summary>
/// <typeparam name="T"></typeparam>
public class VideoContainer<T> : IVideoContainer
{
    public int Id { get; set; }
    public ContainerTypes ContainerType { get; set; }
    public VideoContainerIdentifier ParentObject { get; set; }
    public string Name { get; set; }
    public LocationDefinition LocationDefinition { get; set; }
    public short NumRows { get; set; }
    public short NumColumns { get; set; }
    public IList<IVideoContainer> VideoContainers { get; set; }
}

问题是 SourceContainer 包含我无法访问的新属性 -CctvIdStreamUri

VideoContainers = new List<VideoContainer<SourceContainer>>
    {
        new VideoContainer<SourceContainer>
            {
                Id = 20,
                ParentObject = new VideoContainerIdentifier
                    {
                        Id = 10,
                        ContainerType = ContainerTypes.Perspective
                    },
                ContainerType = ContainerTypes.SourceContainer,
                CctvId = new Guid(),
                StreamUri = new Uri("http://127.0.0.1/somestream"),
                LocationDefinition = new LocationDefinition     // TODO: verify that {x,y} are relative to the perspective
                    {
                        TopLeftX = 0,
                        TopLeftY = 0,
                        WidthPixels = 10,
                        HeightPixels = 10                                                        
                    }

            },

源容器类:

public class SourceContainer : IVideoContainer
{
    /// <summary>
    /// the URI of the stream for this source
    /// </summary>
    public Uri StreamUri { get; set; }

    /// <summary>
    /// the descriptive name of this source
    /// </summary>
    //public string Name { get; set; }

    /// <summary>
    /// optional device id for this source
    /// </summary>
    public Guid? CctvId { get; set; }

    /// <summary>
    /// ctor
    /// </summary>
    public SourceContainer()
    {
        ContainerType = ContainerTypes.SourceContainer;
    }

    public int Id { get; set; }
    public ContainerTypes ContainerType { get; set; }
    public VideoContainerIdentifier ParentObject { get; set; }
    public string Name { get; set; }
    public LocationDefinition LocationDefinition { get; set; }
    public short NumRows { get; set; }
    public short NumColumns { get; set; }
    public IList<IVideoContainer> VideoContainers { get; set; }
}
4

1 回答 1

3

创建一个收集公共属性的接口,让您的三个类从它继承,然后将您的 T 转换为接口以访问属性。

public class VideoContainer: IVideoContainer
{
    public List<IVideoContainer> Children { get; set; }
}

如果您想访问特定于您存储在列表中的对象的属性,只需将其转换为原始对象:

var child = Children.First();
var type = m.GetType();
if(type.Name == "ChildClass")
{
    var container = (ChildClass)child;
    // Now you can access VideoContainer specific properties in `container`.
}
于 2013-10-03T14:02:18.457 回答