的对象类...获取使用属性的字符串不反映
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
你应该考虑使用数组或列表。它们可以被编入索引,似乎更贴近你想要实现的内容。或者可能是一本词典。 –