第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

c#使用一個(gè)變量值從固定列表中設(shè)置第二個(gè)

c#使用一個(gè)變量值從固定列表中設(shè)置第二個(gè)

C#
有只小跳蛙 2022-06-12 14:46:37
我在 ac# .net windows form 應(yīng)用程序中解析一個(gè) CSV 文件,將每一行放入我創(chuàng)建的類中,但是我只需要訪問一些列并且所采用的文件不是標(biāo)準(zhǔn)化的。也就是說,存在的字段數(shù)可能不同,并且列可以出現(xiàn)在任何列中。CSV 示例 1:Position, LOCATION, TAG, NAME, STANDARD, EFFICIENCY, IN USE,,1, AFT-D3, P-D3101A, EQUIPMENT 1, A, 3, TRUE2, AFT-D3, P-D3103A, EQUIPMENT 2, B, 3, FALSE3, AFT-D3, P-D2301A, EQUIPMENT 3, A, 3, TRUE...CSV 示例 2:Position, TAG, STANDARD, NAME, EFFICIENCY, LOCATION, BACKUP, TESTED,,1, P-D3101A, A, EQUIPMENT 1, 3, AFT-D3, FALSE, TRUE2, P-D3103A, A, EQUIPMENT 2, 3, AFT-D3, TRUE, FALSE3, P-D2301A, A, EQUIPMENT 3, 3, AFT-D3, FALSE, TRUE...正如你所看到的,我永遠(yuǎn)不會(huì)知道我必須分析的文件的格式,我唯一確定的是它總是包含我需要的幾列。我對(duì)此的解決方案是要求用戶輸入所需的列并將其設(shè)置為字符串,使用他們的條目將其轉(zhuǎn)換為相應(yīng)的整數(shù),然后我可以將其用作位置。string standardInpt = "";string nameInpt = "";string efficiencyInpt = "";然后用戶將輸入一個(gè)從 A 到 ZZ 的值。int standardLocation = 0;int nameLocation = 0;int efficiencyLocation = 0;提交表單時(shí)。整數(shù)通過運(yùn)行 if else... 語句獲得最終值:if(standard == "A"){  standardLocation = 0;}else if(standard == "B") {  standardLocation = 1;}...等一直運(yùn)行到 if VAR1 == ZZ 然后對(duì) VAR2 和 VAR3 等重復(fù)代碼。我的課程部分看起來像:class Equipment{  public string Standard { get; set;}  public string Name { get; set; }  public int Efficiency { get; set; }  static Equipment FromLine(string line)  {     var data = line.split(',');     return new Equipment()     {      Standard = data[standardLocation],      Name = [nameLocation],      Efficiency = int.Parse(data[efficiencyLocation]),     };   }}我有更多的代碼,但我認(rèn)為這突出了我將使用變量來設(shè)置索引的地方。我對(duì)此很陌生,我希望必須有一種更好的方法來實(shí)現(xiàn)這一點(diǎn),而不必編寫太多可能過多的重復(fù) If Else 邏輯。我正在考慮某種查找表,但我無法弄清楚如何實(shí)現(xiàn)這一點(diǎn),任何關(guān)于我可以查看的指針?
查看完整描述

3 回答

?
慕田峪4524236

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以使用反射和屬性。


將您的樣本,分開寫入DisplayName屬性。


首先調(diào)用GetIndexescsv 頭字符串作為參數(shù),獲取類屬性和 csv 字段的映射字典。


然后調(diào)用FromLine每一行和你剛剛得到的映射字典。


class Equipment

{

    [DisplayName("STND, STANDARD, ST")]

    public string Standard { get; set; }

    [DisplayName("NAME")]

    public string Name { get; set; }

    [DisplayName("EFFICIENCY, EFFI")]

    public int Efficiency { get; set; }

    // You can add any other property


    public static Equipment FromLine(string line, Dictionary<PropertyInfo, int> map)

    {

        var data = line.Split(',').Select(t => t.Trim()).ToArray();

        var ret = new Equipment();

        Type type = typeof(Equipment);

        foreach (PropertyInfo property in type.GetProperties())

        {

            int index = map[property];

            property.SetValue(ret, Convert.ChangeType(data[index],

                property.PropertyType));

        }

        return ret;

    }


    public static Dictionary<PropertyInfo, int> GetIndexes(string headers)

    {

        var headerArray = headers.Split(',').Select(t => t.Trim()).ToArray();

        Type type = typeof(Equipment);

        var ret = new Dictionary<PropertyInfo, int>();

        foreach (PropertyInfo property in type.GetProperties())

        {

            var fieldNames = property.GetCustomAttribute<DisplayNameAttribute>()

                .DisplayName.Split(',').Select(t => t.Trim()).ToArray();

            for (int i = 0; i < headerArray.Length; ++i)

            {

                if (!fieldNames.Contains(headerArray[i])) continue;

                ret[property] = i;

                break;

            }

        }

        return ret;

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-06-12
?
開滿天機(jī)

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊

您可以通過在標(biāo)題中查找列的索引來使其自動(dòng)化,然后使用它們從其余行的正確位置讀取值:


class EquipmentParser {

    public IList<Equipment> Parse(string[] input) {

        var result = new List<Equipment>();


        var header = input[0].Split(',').Select(t => t.Trim().ToLower()).ToList();

        var standardPosition = GetIndexOf(header, "std", "standard", "st");

        var namePosition = GetIndexOf(header, "name", "nm");

        var efficiencyPosition = GetIndexOf(header, "efficiency", "eff");


        foreach (var s in input.Skip(1)) {

            var line = s.Split(',');

            result.Add(new Equipment {

                Standard = line[standardPosition].Trim(),

                Name = line[namePosition].Trim(),

                Efficiency = int.Parse(line[efficiencyPosition])

            });

        }


        return result;

    }


    private int GetIndexOf(IList<string> input, params string[] needles) {

        return Array.FindIndex(input.ToArray(), needles.Contains);

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-06-12
?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

如果有幫助,試試這個(gè):


    public int GetIndex(string input)

    {

        input = input.ToUpper();

        char low = input[input.Length - 1];

        char? high = input.Length == 2 ? input[0] : (char?)null;

        int indexLow = low - 'A';

        int? indexHigh = high.HasValue ? high.Value - 'A' : (int?)null;

        return (indexHigh.HasValue ? (indexHigh.Value + 1) * 26 : 0) + indexLow;

    }


查看完整回答
反對(duì) 回復(fù) 2022-06-12
  • 3 回答
  • 0 關(guān)注
  • 132 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)