C# 当对象';s属性更改

C# 当对象';s属性更改,c#,.net,C#,.net,我有一个如下所示的对象模型: public class MyModel { public List<MyOtherObject> TheListOfOtherObjects { get; set; } public List<int> MyOtherObjectIDs { get; set; } public void GetListOfMyOtherObjectIDs() { // function that extr

我有一个如下所示的对象模型:

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
    }
}
公共类MyModel
{
public List TheListOfOtherObjects{get;set;}
公共列表MyOtherObjectIDs{get;set;}
public void GetListofMyotherObjectId()的列表
{
//函数,用于提取中对象的ID
//列表并将其指定给MyotherObjectId
}
}
目前,我有一些代码,当从查询中填充ListFootherObjects时,它执行
GetListOfMyotherObjects
。现在,我在填充此列表的代码中找到了另一个位置,当它填充此列表时,它还需要执行
getListofMyotherObjectId
函数


是否有一种方法可以使此过程自动进行,以便在弹出
ListofOtherObjects
时,无论哪种代码触发它,对象模型都会自动执行
GetListofMyotherObjects

使用您自己的
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
    }
}
公共类MyModel
{
私有列表_TheListOfOtherObjects;
公共列表列表列表其他对象{
获取{return\u TheListOfOtherObjects;}
设置{TheListFotherObjects=value;GetListOfMyotherObjects();}
}
公共列表MyOtherObjectIDs{get;set;}
public void GetListofMyotherObjectId()的列表
{
//函数,用于提取中对象的ID
//列表并将其指定给MyotherObjectId
}
}

使用您自己的
设置
访问者:

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
    }
}
公共类MyModel
{
私有列表_TheListOfOtherObjects;
公共列表列表列表其他对象{
获取{return\u TheListOfOtherObjects;}
设置{TheListFotherObjects=value;GetListOfMyotherObjects();}
}
公共列表MyOtherObjectIDs{get;set;}
public void GetListofMyotherObjectId()的列表
{
//函数,用于提取中对象的ID
//列表并将其指定给MyotherObjectId
}
}


该方法的返回类型是什么?它当前的签名看起来像是一个构造函数,实际上并不是。@abatishchev:对不起,忘记了返回类型;它是无效的。您是否搜索并阅读了有关
INotifyPropertyChanged
ObservableCollection
?你肯定需要,它会解决你的问题。不,我不知道怎么做,这就是为什么我问如何实现它。方法的返回类型是什么?它当前的签名看起来像是一个构造函数,实际上并不是。@abatishchev:对不起,忘记了返回类型;它是无效的。您是否搜索并阅读了有关
INotifyPropertyChanged
ObservableCollection
?你肯定需要,它会解决你的问题。不,我不知道怎么做,这就是为什么我问如何实施它。啊,好的!!!!!这意味着什么:设置{TheListOfOtherObjects=value;?,如果我删除它并只保留设置{getListofMyotherivjections();}它会工作吗?我也可以避免使用私有列表属性吗?+1如果要通过
listofotherobjects
set方法完全管理它,我建议在
myotherobjects
上设置一个
private set
。@frenchie您可能想了解一下(您在OP中使用的)和(我使用的)唯一的危险是,有人可以从
列表中添加/删除其他对象,而不会调用
GetListofMyotherObjectId
,从而使这两个列表不同步。@mikez我想它们都将引用同一个对象,因此它们的内容基本相同。但是你是对的,这不适用于什么时候一些代码从
列表中添加/删除
。考虑一个自定义包装类…啊,好吧!!!!!这是什么意思:设置{TheListOfOtherObjects=value;?如果我删除它,只保留设置{getListofMyotherIVjections();}它会工作吗?我也可以避免使用私有列表属性吗?+1如果要通过
listofotherobjects
set方法完全管理它,我建议在
myotherobjects
上设置一个
private set
。@frenchie您可能想了解一下(您在OP中使用的)和(我使用的)唯一的危险是,有人可以从
列表中添加/删除其他对象,而不会调用
GetListofMyotherObjectId
,从而使这两个列表不同步。@mikez我想它们都将引用同一个对象,因此它们的内容基本相同。但是你是对的,这不适用于什么时候一些代码从
列表中添加/删除。。。