[英]Update Property When Another Property Changed
I want to update Name
property of a class whenever matching ID
changes, I get the matching Name
from web reference.我想在匹配
ID
更改时更新 class 的Name
属性,我从 web 参考中获取匹配的Name
。
For example:例如:
class Request
{
private int xID;
public int XID
{
get =>
set
{
xID = value;
XName = value;
}
}
private string xName;
public string XName
{
get => xName
set
{
xName = new FillUser(value).FullUserName // the usage of the WebReference,
// create an object with all user credentials
}
}
public int YID { get; set; }
public string YName { get; set; }
}
So I have multiple ID's and all of them has matching name and all of them will work the same.所以我有多个 ID,它们都有匹配的名称,并且它们都将工作相同。
Except ID value will be change depending on the prop.除了 ID 值会根据道具而改变。
Looking for best practice way to solve this problem - thanks a lot.寻找解决这个问题的最佳实践方法 - 非常感谢。
You should not do XName = value;
你不应该做
XName = value;
You write a method say ChangeId and then put all the logic in it.你写一个方法说 ChangeId,然后把所有的逻辑都放进去。 The other IDs props should remain get only.
其他 ID 道具应保持仅获取。
void Changex(into xid)
{
xId = xid;
xName = Full User.GetUserNameById(xid);
}
The solution that I find It's creating custom Attribute for those property, and then under that category you multiple ways.我找到的解决方案是为这些属性创建自定义属性,然后在该类别下有多种方式。
You can send argument to the prop attribute with name of the dependent attribute,您可以使用依赖属性的名称将参数发送到 prop 属性,
Or generic method, and etc.或泛型方法等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.