3

C# で、コレクションを持つオブジェクトがある場合、コレクションを含むオブジェクトを取得することはできますか?

次に例を示します。

public class TestObject
{
    public string name { get; set; }
    public TestObjectCollection testObjects{ get; set; } 
}

TestObjectCollectionコレクションは から継承され、のCollectionBaseコレクションですTestObjects

実装例を次に示します。

  • ATestObjectは次の名前で作成されます"Test1"
  • TestObject名前の に は"Test1"の名前の がありますTestObjectCollectionTestObject"Test2"

TestObjectの名前のを持っている場合、どうすれば の名前の"Test2"を入手できますかTestObject"Test1"

ありがとう

4

4 に答える 4

2

これを行う唯一の方法は、子オブジェクトで親への参照を保持することです。子オブジェクトの作成中にこれを行うことができます。

this.testObjects = new TestObjectCollection(this);

次に、TestObjectCollection のコンストラクターで:

public TestObject ParentObject { get; set; }

public TestObjectCollection(TestObject parent)
{
    ParentObject = parent;
    ...
}
于 2015-08-13T02:39:55.383 に答える
0

その親子関係を明示的にコーディングしない限り(Yogeshの回答のように)、「その」親を見つける方法はありません-主にそのような親が複数存在する可能性があるためです:

public class TestObject
{
    public string name { get; set; }
    public TestObjectCollection testObjects{ get; set; } 
}
public class TestObjectCollection : CollectionBase
{
    public void Add(TestObject to)
    {
        this.List.Add(to);
    }
}
void Main()
{
    TestObjectCollection children = new TestObjectCollection();
    TestObject child = new TestObject { name = "child" };
    children.Add(child);

    TestObject parent = new TestObject { name = "parent", testObjects = children }; 
    TestObject otherParent = new TestObject { name = "otherParent", testObjects = children };   
    TestObject stepParent = new TestObject { name = "stepParent", testObjects = children }; 
    TestObject inLocoParentis = new TestObject { name = "inLocoParentis", testObjects = children };
    // and we can keep going on and on and on ...   
}
于 2015-08-13T03:13:59.170 に答える
0

コンストラクターで参照を渡したくない場合は、静的ディクショナリを使用して TestObject インスタンスを追跡し、TestObjectCollection にその静的ディクショナリから遅延読み込み方式でその親を検索させることができます。

例えば

public class TestObject
{
    /// <summary>
    /// Keep a list of all the instances of TestObject's that are created.
    /// </summary>
    internal static Dictionary<Guid, TestObject> _collections = new Dictionary<Guid, TestObject>();

    /// <summary>
    /// An ID to uniquely identify an instance of a TestObject
    /// </summary>
    public Guid ID { get; private set; }

    /// <summary>
    /// A reference to the collection which will be set in the constructor
    /// </summary>
    public TestObjectCollection TestObjects { get; private set; }

    public TestObject()
    {
        //generate the unique id
        this.ID = Guid.NewGuid();
        this.TestObjects = new TestObjectCollection();
        //add this testobject to the List of test objects.
        _collections.Add(this.ID, this);
    }

    /// <summary>
    /// Destructor, kill the TestObject from the list of TestObject's.
    /// </summary>
    ~TestObject()
    {
        if (_collections.ContainsKey(this.ID))
        {
            _collections.Remove(this.ID);
        }
    }
}

public class TestObjectCollection : IEnumerable<TestObject>
{
    private List<TestObject> _testObjects = new List<TestObject>();

    public Guid ID { get; private set; }

    public TestObject this[int i]
    {
        get
        {
            return _testObjects[i];
        }
    }

    private TestObject _Parent = null;
    public TestObject Parent
    {
        get
        {
            if (_Parent == null)
            {
                _Parent = TestObject._collections.Values.Where(p => p.TestObjects.ID == this.ID).FirstOrDefault();
            }
            return _Parent;
        }
    }

    public TestObjectCollection()
    {
        this.ID = Guid.NewGuid();
    }

    public void Add(TestObject newObject)
    {
        if (newObject != null)
            _testObjects.Add(newObject);
    }

    public IEnumerator<TestObject> GetEnumerator()
    {
        return _testObjects.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return _testObjects.GetEnumerator();
    }
}

テスト中...

class Program
{
    static void Main(string[] args)
    {
        TestObject tObject = new TestObject();
        Console.WriteLine("TestObject ID: " + tObject.ID);
        Console.WriteLine("TestObject TestObjectCollection ID: " + tObject.TestObjects.ID);
        Console.WriteLine("TestObject TestObjectCollection Parent ID: " + tObject.TestObjects.Parent.ID);
        Console.WriteLine("Press any key...");

        Console.ReadKey(true);
    }
}

したがって、これが行うことは、それ自体に GUID ID を与える TestObject のコンストラクターにあります。次に、TestObjectCollection のインスタンスを作成します。

TestObjectCollection のコンストラクターでは、それ自体に GUID ID を与えます。

TestObject のコンストラクターに戻り、作成したばかりのコレクションに TestObjects を設定し、それ自体への参照を静的な TestObjects の Dictionary に追加します。TestObject の ID を上記の Dictionary のキーとして使用します。

次に、TestObjectCollection で、呼び出されるまでそれ自体を設定しないプロパティを使用してその静的辞書で検索することにより、親コレクションを取得します (TestObject コンストラクターがまだ参照を追加していないため、コンストラクターで決定できないため) )。

    private TestObject _Parent = null;
    public TestObject Parent
    {
        get
        {
            if (_Parent == null)
            {
                _Parent = TestObject._collections.Values.Where(p => p.TestObjects.ID == this.ID).FirstOrDefault();
            }
            return _Parent;
        }
    }
于 2015-08-13T03:31:51.937 に答える