두 개체 MetaItems 및 Items 있습니다.C# 제네릭과 컬렉션
MetaItem은 개체의 템플릿이며 Item에는 실제 값이 들어 있습니다. 예를 들어 "Department"는 메타 항목으로 취급되고 "Sales", "UK Region", "Asia Region"은 항목으로 취급됩니다.
또한 이러한 메타 항목 및 항목에 대해 부모 - 자식 관계를 유지하려고합니다. 같은 대한
나는 다음 한 코드 - 테스트 클래스에서
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.
}
}
, 내가 부모 - 자식 관계를 buidling하고 때, 나는 같은 항목을 추가 할 수 있습니다 -
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
{
}
}
다음은 내 테스트 클래스 메타 아이템 오브젝트의 아이 오브젝트 여기에 컴파일 오류가 발생합니다.
누군가 내게 도움이 될 수 있습니까?
-Regards는 라지
메타 아이템이 다른 아이템을 포함하는 아이템 일 경우, 실제로는 아이템인데, 여기서'T = 아이템 '은 아닌가? –
질문과 관련이 없지만 Entity.Id 및 Name의 경우 자동 구현 속성은 친구입니다. – dahlbyk