C# 泛型和集合

发布于 2024-09-06 14:20:44 字数 2887 浏览 5 评论 0 原文

我有两个对象 MetaItems 和 Items。

MetaItem 是对象的模板,而 Items 包含实际值。例如,“部门”被视为元项目,“销售”、“英国区域”、“亚洲区域”被视为项目。

此外,我想维持这些元项目和项目的父子关系。

我有以下相同的代码 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfApplication12
{
    public interface IEntity
    {
        int Id { get; set; }

        string Name { get; set; }
    }

    public interface IHierachy<T>  
    {
        IHierachy<T> Parent { get; }

        List<IHierachy<T>> ChildItems { get; }

        List<IHierachy<T>> LinkedItems { get; }

    }

    public class Entity : IHierachy<IEntity>, IEntity
    {

        #region IObject Members

        private int _id;
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }

        private string _name;

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        #endregion

        #region IHierachy<IEntity> Members

        public IHierachy<IEntity> _parent;
        public IHierachy<IEntity> Parent
        {
            get
            {
                return _parent;
            }
        }

        private List<IHierachy<IEntity>> _childItems;

        public List<IHierachy<IEntity>> ChildItems
        {
            get
            {
                if (_childItems == null)
                {
                    _childItems = new List<IHierachy<IEntity>>();
                }
                return _childItems;
            }
        }

        private List<IHierachy<IEntity>> _linkedItems;

        public List<IHierachy<IEntity>> LinkedItems
        {
            get
            {
                if (_linkedItems == null)
                {
                    _linkedItems = new List<IHierachy<IEntity>>();
                }
                return _linkedItems;
            }
        }
        #endregion
    }


    public class Item : Entity
    {
    }

    public class MetaItem : Entity
    {
    }

}

以下是我的测试类 -

public class Test
{
    public void Test1()
    {
        MetaItem meta1 = new MetaItem() { Id = 1, Name = "MetaItem1"};

        MetaItem meta2 = new MetaItem() { Id = 1, Name = "MetaItem 1.1"};

        Item meta3 = new Item() { Id = 101, Name = "Item 1" };


        **meta1.ChildItems.Add(meta3);** // this line should not compile.
        meta1.ChildItems.Add(meta2)  // This is valid and gets compiled.
    }
}

在测试类中,当我构建父子关系时,我可以添加项目作为元项目对象的子对象。这里我想要生成编译错误。

有人可以帮助我实现这一目标吗?

-问候 拉吉

I have two objects MetaItems and Items.

MetaItem is template for objects and Items contains actual values. For example "Department" is treated as meta-item and "Sales", "UK Region", "Asia Region" are treated as items.

Additionally I want to maintain parent-child relation on these meta-items and items.

I have following code for same -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfApplication12
{
    public interface IEntity
    {
        int Id { get; set; }

        string Name { get; set; }
    }

    public interface IHierachy<T>  
    {
        IHierachy<T> Parent { get; }

        List<IHierachy<T>> ChildItems { get; }

        List<IHierachy<T>> LinkedItems { get; }

    }

    public class Entity : IHierachy<IEntity>, IEntity
    {

        #region IObject Members

        private int _id;
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }

        private string _name;

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        #endregion

        #region IHierachy<IEntity> Members

        public IHierachy<IEntity> _parent;
        public IHierachy<IEntity> Parent
        {
            get
            {
                return _parent;
            }
        }

        private List<IHierachy<IEntity>> _childItems;

        public List<IHierachy<IEntity>> ChildItems
        {
            get
            {
                if (_childItems == null)
                {
                    _childItems = new List<IHierachy<IEntity>>();
                }
                return _childItems;
            }
        }

        private List<IHierachy<IEntity>> _linkedItems;

        public List<IHierachy<IEntity>> LinkedItems
        {
            get
            {
                if (_linkedItems == null)
                {
                    _linkedItems = new List<IHierachy<IEntity>>();
                }
                return _linkedItems;
            }
        }
        #endregion
    }


    public class Item : Entity
    {
    }

    public class MetaItem : Entity
    {
    }

}

Following is my test class -

public class Test
{
    public void Test1()
    {
        MetaItem meta1 = new MetaItem() { Id = 1, Name = "MetaItem1"};

        MetaItem meta2 = new MetaItem() { Id = 1, Name = "MetaItem 1.1"};

        Item meta3 = new Item() { Id = 101, Name = "Item 1" };


        **meta1.ChildItems.Add(meta3);** // this line should not compile.
        meta1.ChildItems.Add(meta2)  // This is valid and gets compiled.
    }
}

In the test class, when I am buidling parent-child relation, I can add item as a child object for meta-item object. Here I want compilation error to be generated.

Can someone help me in achiving this.

-Regards
Raj

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

也只是曾经 2024-09-13 14:20:44

代码正在编译,因为 ChildItems 将是 IList,其中包括 ItemMetaItem。如果您要使 Entity 通用:

public class Entity<T> : IHierachy<T>, IEntity where T : IEntity { ... }

那么您将像这样定义 ItemMetaItem

public class Item : Entity<Item> { }

public class MetaItem : Entity<MetaItem> { }

在这种情况下,它们的 ChildItems 将是正确的、更受限制的类型。

The code is compiling because ChildItems will be IList<Entity>, which includes both Item and MetaItem. If you were to make Entity generic:

public class Entity<T> : IHierachy<T>, IEntity where T : IEntity { ... }

Then you would define Item and MetaItem like this:

public class Item : Entity<Item> { }

public class MetaItem : Entity<MetaItem> { }

In which case their ChildItems would be of the correct, more restricted, type.

呆头 2024-09-13 14:20:44

你为什么不认为该行应该编译?它看起来完全有效。

ChildItems 列表是公开的。如果您不希望它们能够添加到列表中,那么您需要包装自己的集合或使用 ReadOnlyCollection>

哦,你的问题已经解决了。我认为解决方案是使实体类通用。

using System.Collections.Generic;

namespace WpfApplication12
{
    public interface IEntity
    {
        int Id { get; set; }
        string Name { get; set; }
    }

    public interface IHierachy<T>
    {
        IHierachy<T> Parent { get; }
        List<IHierachy<T>> ChildItems { get; }
        List<IHierachy<T>> LinkedItems { get; }
    }

    public class Entity<T> : IHierachy<T>, IEntity
    {
        private int _id;
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public IHierachy<T> _parent;
        public IHierachy<T> Parent
        {
            get
            {
                return _parent;
            }
        }

        private List<IHierachy<T>> _childItems;
        public List<IHierachy<T>> ChildItems
        {
            get
            {
                if( _childItems == null )
                {
                    _childItems = new List<IHierachy<T>>();
                }
                return _childItems;
            }
        }

        private List<IHierachy<T>> _linkedItems;
        public List<IHierachy<T>> LinkedItems
        {
            get
            {
                if( _linkedItems == null )
                {
                    _linkedItems = new List<IHierachy<T>>();
                }
                return _linkedItems;
            }
        }
    }


    public class Item : Entity<Item>
    {
    }

    public class MetaItem : Entity<MetaItem>
    {
    }

    public class Test
    {
        public void Test1()
        {
            MetaItem meta1 = new MetaItem() { Id = 1, Name = "MetaItem1"};
            MetaItem meta2 = new MetaItem() { Id = 1, Name = "MetaItem 1.1"};
            Item meta3 = new Item() { Id = 101, Name = "Item 1" };

            meta1.ChildItems.Add(meta3); // this line should not compile.
            meta1.ChildItems.Add( meta2 );  // This is valid and gets compiled.
        }
    }

}

Why don't you think that line should compile? It looks completely valid.

The ChildItems list is public. If you don't want them to be able to add to the list then you will need to wrap your own collection or use the ReadOnlyCollection<IHierachy<IEntity>>.

Oh, you have fixed your question. I think the solution is to make the Entity class generic.

using System.Collections.Generic;

namespace WpfApplication12
{
    public interface IEntity
    {
        int Id { get; set; }
        string Name { get; set; }
    }

    public interface IHierachy<T>
    {
        IHierachy<T> Parent { get; }
        List<IHierachy<T>> ChildItems { get; }
        List<IHierachy<T>> LinkedItems { get; }
    }

    public class Entity<T> : IHierachy<T>, IEntity
    {
        private int _id;
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public IHierachy<T> _parent;
        public IHierachy<T> Parent
        {
            get
            {
                return _parent;
            }
        }

        private List<IHierachy<T>> _childItems;
        public List<IHierachy<T>> ChildItems
        {
            get
            {
                if( _childItems == null )
                {
                    _childItems = new List<IHierachy<T>>();
                }
                return _childItems;
            }
        }

        private List<IHierachy<T>> _linkedItems;
        public List<IHierachy<T>> LinkedItems
        {
            get
            {
                if( _linkedItems == null )
                {
                    _linkedItems = new List<IHierachy<T>>();
                }
                return _linkedItems;
            }
        }
    }


    public class Item : Entity<Item>
    {
    }

    public class MetaItem : Entity<MetaItem>
    {
    }

    public class Test
    {
        public void Test1()
        {
            MetaItem meta1 = new MetaItem() { Id = 1, Name = "MetaItem1"};
            MetaItem meta2 = new MetaItem() { Id = 1, Name = "MetaItem 1.1"};
            Item meta3 = new Item() { Id = 101, Name = "Item 1" };

            meta1.ChildItems.Add(meta3); // this line should not compile.
            meta1.ChildItems.Add( meta2 );  // This is valid and gets compiled.
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文