我有一个如下所示的对象模型:
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
?
答案 0 :(得分: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
}
}