c# - 使用不带反射的字符串获取属性

标签 c# winforms reflection interface

项目等级...

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 语句列表中。

我一直在搞反射,但想避免这种情况,我在某​​处读到你可以用接口(interface)来做到这一点(使用它们来公开属性),但我不知道如何做。

如果 Reflection 是最好的选择,有人可以告诉我两种获取属性的方法和另一种设置它的方法吗?

最佳答案

我建议您采用不同的方法。 IMO 反射(reflection)不是解决此类情况的方法。

以此为起点,然后以此为基础进行构建——当然,还要重新设计/重构其他部分:

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

关于c# - 使用不带反射的字符串获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35773246/

相关文章:

c# - 如何访问传递给构造函数的 ref bool 变量?

c++ - 为什么许多编译语言不包含编译时反射?

java - Class API 中的 getDeclaredConstructors 和 getConstructors 有什么区别?

c# - 使用可能不存在的反射设置属性

c# - 当基类型列表包含基类和接口(interface)时,基类必须在列表中排在第一位

c# - 编写高度用户可扩展的 C# 应用程序的最佳实践

c# - 每个 <% 的含义列表(<%#、<%= 等...)

c# - 营销人员自定义提交操作错误消息的 Web 表单

c# - 如何将 .Net 框架先决条件添加到设置安装

c# - 为什么 WebBrowser DocumentCompleted 仅在加载所有图像时触发?