0

我有一个看起来像这样的对象模型:

public class MyModel
{
    public List<MyOtherObject> TheListOfOtherObjects { get; set; }

    public List<int> MyOtherObjectIDs { get; set; }

    public void GetListOfMyOtherObjectIDs() 
    {
       // function that extracts the IDs of objects in
       // the list and assigns it to MyOtherObjectIDs
    }
}

目前,我有一些代码在从查询中填充GetListOfMyOtherObjectIDs时执行。TheListOfOtherObjects现在我在代码中的另一个位置也填充了这个列表,当它出现时,它还需要执行该GetListOfMyOtherObjectIDs函数。

有没有办法使这个过程自动化,以便在TheListOfOtherObjects填充时,无论哪个代码触发它,对象模型都会自动执行GetListOfMyOtherObjectIDs

4

1 回答 1

3

使用您自己的set访问器:

public class MyModel
{
    private List<MyOtherObject> _TheListOfOtherObjects;
    public List<MyOtherObject> TheListOfOtherObjects {
        get { return _TheListOfOtherObjects; }
        set { _TheListOfOtherObjects = value; GetListOfMyOtherObjectIDs(); }
    }

    public List<int> MyOtherObjectIDs { get; set; }

    public void GetListOfMyOtherObjectIDs() 
    {
       // function that extracts the IDs of objects in
       // the list and assigns it to MyOtherObjectIDs
    }
}
于 2012-10-19T02:58:45.593 回答