的對象類...獲取使用屬性的字符串不反映
class Room
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 { get; set; }
public Room()
{
this.Value1 = "one";
this.Value2 = "two";
this.Value3 = "three";
}
}
class Building
{
public Room Room-Bob { get; set; }
public Room Room-Steve{ get; set; }
public Building()
{
this.Room-Bob = new Room();
this.Room-Steve = new Room();
}
}
class Street
{
public Building Building-Black{ get; set; }
public Building Building-Blue { get; set; }
public Building Building-Yellow { get; set; }
public Building Building-White { get; set; }
public Street()
{
this.Building-Black = new Building();
this.Building-Blue = new Building();
this.Building-Yellow = new Building();
this.Building-White = new Building();
}
}
我現在使用的是什麼來獲取值...
class go
{
public void go()
{
string SelectedValue = "";
Street s = new Street();
string PathToProperty = "s.Building1.Room1.value1";
if(PathToProperty == "s.Building1.Room1.value1") { SelectedValue = s.Building1.Room1.Value1; }
if (PathToProperty == "s.Building1.Room1.value2") { SelectedValue = s.Building1.Room1.Value2; }
}
}
我怎麼想獲取值...或者類似的東西
string PathToProperty = "s.Building1.Room1.value1";
SelectedValue = PathToProperty;
我也想這樣設置屬性...
string PathToProperty =「s.Building1.Room1.value1」;
SelectedValue = PathToProperty;
原因是我通過將一堆組合框中的文本串在一起來製作PathToProperty。最終,我想避免隨着組合框內的選項的增加,不得不添加到我的IF語句列表中。
我一直在反思,但想避免這種情況,我讀了一個地方,你可以用接口做到這一點(使用它們來暴露屬性),但我不知道如何。
如果反射是最好的選擇,有人可以告訴我兩種方法來獲得財產,另一種設置它?
反思是明確設計做你想做什麼工具。你爲什麼認爲這是一個不好的選擇? – Luaan
A-我不能讓它正常工作B-每個人都說如果反射是最好的方式,那麼我應該避免它,那麼我將如何獲得和設置屬性 – user3755946
你應該考慮使用數組或列表。它們可以被編入索引,似乎更貼近你想要實現的內容。或者可能是一本詞典。 –