[英]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_1
和Station_2
類,而只使用@Fildor建議的Dictionary<string, int>
或Dictionary<string, object>
(如果您需要存儲沒有值的鍵)呢?
反射很昂貴,僅在絕對必要時才應使用。 正如您到目前為止所解釋的,您的情況似乎並不需要它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.