[英]C# setting multiple properties through a single assignment
我有一個屬性被分配為:
public string propertyA{get;set;}
當此屬性設置為在 class 中分配另一個屬性時,我想自動調用一個方法。 觸發通話的最佳方式?
您不必使用只是簡寫的語法。 如果你擴展它,你可以在 setter 中做任何你喜歡的事情。
public string PropertyA
{
get { return a; }
set
{
a = value;
doStuff();
}
}
我認為您將不得不 go 回到以老式方式處理您的財產。
private string _PropertyA;
public string propertyA
{
get
{
return _PropertyA;
}
set
{
_PropertyA=value;
//Set other parameter
}
}
展開屬性設置器並分配其他屬性。
手動添加支持字段並提供一些代碼以在設置器中執行您想要的操作。
private string propertyA;
public string PropertyA
{
get { return this.propertyA; }
set
{
this.propertyA = value;
this.propertyB = value + "B";
}
}
定義二傳手。
在它里面要么觸發一個事件,要么直接分配另一個屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.