使用不带反射的字符串获取属性(Get Property using a string without reflection)

编程入门 行业动态 更新时间:2024-10-10 12:20:08
使用不带反射的字符串获取属性(Get Property using a string without reflection)

对象类...

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;

我也想设置这样的属性...

字符串PathToProperty =“s.Building1.Room1.value1”; SelectedValue = PathToProperty;

原因是我通过将一堆组合框中的文本串在一起来制作PathToProperty。 最终,我想避免随着组合框内的选项的增加,不得不添加到我的IF语句列表中。

我一直在讨论反射,但想避免这种情况,我读了一些地方可以用接口来做到这一点(使用它们来公开属性),但我不知道如何。

如果Reflection是这个最好的选择,有人可以告诉我两种获得财产的方法,另一种方法来设置它?

The Object Classes...

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(); } }

What I am currently using to get the values...

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; } } }

How I would like to get the values... or something similar

string PathToProperty = "s.Building1.Room1.value1"; SelectedValue = PathToProperty;

I would also like to set the property like this...

string PathToProperty = "s.Building1.Room1.value1"; SelectedValue = PathToProperty;

The reason being I am making the the PathToProperty by stringing together the text from a bunch of combo boxes. Ultimately I want to avoid having to add to my list of IF statements as the options inside the combo boxes increase.

I have being messing around with reflection but would like to avoid this , I read somewhere you can do this with interfaces (using them to expose properties) but I do not know how.

If Reflection is the best choice for this can someone show me 2 methods of to get the property and another to set it?

最满意答案

我建议你采取不同的方法。 IMO反思不是这种情况的方法。

从这开始,然后从中构建 - 重新设计/重构其他部分,当然:

class Room { // same as yours } class Building { public List<Room> Rooms { get; set; } public Building() { Rooms = new List<Room>(); Rooms.Add(new Room()); Rooms.Add(new Room()); // get "room #x" -> var room = objBuilding.Rooms[x]; // get "room #x in building #i" -> var room = objStreet.Buildings[i].Rooms[x]; } } class Street { public List<Building> Buildings { get; set; } public Street () { Buildings = new List<Building>(); Buildings.Add(new Building()); Buildings.Add(new Building()); Buildings.Add(new Building()); Buildings.Add(new Building()); // get "building #i" -> var building = objStreet.Buildings[i]; } }

I'd suggest you take a different approach. IMO reflection is not the way to go for these kinds of situations.

Start with this, then build from it - redesigning/refactoring other parts as well, of course:

class Room { // same as yours } class Building { public List<Room> Rooms { get; set; } public Building() { Rooms = new List<Room>(); Rooms.Add(new Room()); Rooms.Add(new Room()); // get "room #x" -> var room = objBuilding.Rooms[x]; // get "room #x in building #i" -> var room = objStreet.Buildings[i].Rooms[x]; } } class Street { public List<Building> Buildings { get; set; } public Street () { Buildings = new List<Building>(); Buildings.Add(new Building()); Buildings.Add(new Building()); Buildings.Add(new Building()); Buildings.Add(new Building()); // get "building #i" -> var building = objStreet.Buildings[i]; } }

更多推荐

本文发布于:2023-08-04 21:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422341.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不带   字符串   反射   属性   reflection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!