繁体   English   中英

通过没有类名称的字符串设置和获取嵌套类的值

[英]Set and get value of the Nested Class by string without class name

我有很多txt文件,其中包含很多数据。 例如:

1.txt
parameter1_1="[1 3 21 12 42]"
parameter2_1="[4 2 28 72 46]"
parameter2_2="[2 6 46 75 76]"
58.txt
parameter1_1="[9 8 98 87 89]"
parameter1_2="[2 4 11 42 62]"
parameter2_1="[9 4 25 67 56]"
parameter2_2="[7 6 87 79 75]"

在第一个txt文件中,所有参数的第一个值都是线圈数据。 第二值是第二线圈数据。 我想将所有值放入一个类/对象,其中包含所有值,就像您在图片上看到的那样。

在此处输入图片说明

当我上传对象时,索引不能是滑动的,因此,如果我从集合中选择索引1,那么我将获得正确的数据。

如您所见,我创建了一个类,但无法通过字符串获取嵌套的类属性。

我下一堂课

    public class Coil
{
    public string ID { get; set; }
}

public class FailStation : Coil
{
    public Station_1 Station_1 = new Station_1();
    public Station_2 Station_2 = new Station_2();
}

public class Station_1
{
    public string parameter1_1{ get; set; }
    public string parameter1_2 { get; set; }
}
public class Station_2
{
    public string parameter2_1{ get; set; }
    public string parameter2_2 { get; set; }
}

我想设置并获得类似或类似的值:

FailStationcoil coil = new FailStation();
List<FailStation> AllCoil = new List<FailStation>();
coil["parameter1_1"] = "value"; //or
coil.SetPropValue("parameter1_1", "value");
coil["ID"]="1";
AllCoil.Add(coil);

似乎您的参数名称已经告诉您它是什么Station ,除非我误读了它。

parameter1_1 =>第一个数字告诉您Station_{n} ,第二个数字告诉您cam_term_{n}_val

那么,为什么不放弃Station_1Station_2类,而只使用@Fildor建议的Dictionary<string, int>Dictionary<string, object> (如果您需要存储没有值的键)呢?

反射很昂贵,仅在绝对必要时才应使用。 正如您到目前为止所解释的,您的情况似乎并不需要它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM