Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新包含任何类型的各种键和结构的字典_C#_Dictionary_Struct_Elements - Fatal编程技术网

C# 更新包含任何类型的各种键和结构的字典

C# 更新包含任何类型的各种键和结构的字典,c#,dictionary,struct,elements,C#,Dictionary,Struct,Elements,我正在尝试将字符串集合类型转换为包含各种类型的键和值的字典(值在大多数情况下都是一个结构)。 字符串集合中的每个字符串将包含第一个元素中的键,字符串的其余部分将包含struct元素(字典的值),当然字符串将包含struct中每个元素的正确类型我们只需要解码struct元素的类型。。。 我试着这么做,但我遇到了死胡同,没有可能。。。 我试着用谷歌搜索它,但我的案例很难,因为它应该与我程序中的很多案例相匹配。。。 这就是我试图做的 private static void LoadDicti

我正在尝试将字符串集合类型转换为包含各种类型的键和值的字典(值在大多数情况下都是一个结构)。 字符串集合中的每个字符串将包含第一个元素中的键,字符串的其余部分将包含struct元素(字典的值),当然字符串将包含struct中每个元素的正确类型我们只需要解码struct元素的类型。。。 我试着这么做,但我遇到了死胡同,没有可能。。。 我试着用谷歌搜索它,但我的案例很难,因为它应该与我程序中的很多案例相匹配。。。 这就是我试图做的

     private static void LoadDictionaryFromString<TKey,TValue>(ref IDictionary<TKey,TValue> argetDict,StringCollection SourceString)
    {
        TargetDict.Clear();

        foreach(string strData in SourceString)
        {
            string[] strElements = strData.Split(';');
            int m = 1;
            // i must initialize 
            object Key = new object();  // when i try =>   TKey Key = new TKey() it's not working
            object Value = new object();   // the same like previous line...
            foreach (var field in typeof(TValue).GetFields())
            {
                // I can't work this out.... i stuck here
                // in this line i'm getting an exception -> out of index...
                TypeDescriptor.GetProperties(TargetDict.Values)[m].SetValue(Value , strElements[m]);
            }
            TypeDescriptor.GetProperties(TargetDict.Keys)[0].SetValue(Key, strElements[0]);

            TargetDict.Add((TKey)Key,(TValue)Value);
        }
    }
现在让我们假设有两条记录包含这些值:

OrderID = 155 (for the key part) and for the struct : Apple , 200, 1, Accepted, 250, 12/05/2013 10:00:00
OrderID = 156 (for the key part) and for the struct : IBM, 105, 1, Executed, 200, 12/05/2013 12:34:10
因此,保存的字符串集合将是name StringCollection strCollection

"155;Apple;200;1;Accepted;250;12/05/2013 10:00:00
156;IBM;105;1;Executed;200;12/05/2013 12:34:10"
现在,过了一会儿,我从上面的种类中保存了一个字符串集合,我知道它包含上面指定的结构,所以我像这样运行函数:

    IDictionary<int, sampleStruct> DictToTarget=new IDictionary<int, sampleStruct>();

    // the Dictionary will be cleared and will contain the data as saved in the string collection
    // the stringCollection will contain the data above
    LoadDictionaryFromString<int, sampleStruct>(ref DictToTarget, strCollection);
IDictionary DictToTarget=新的IDictionary();
//字典将被清除,并将包含保存在字符串集合中的数据
//stringCollection将包含上述数据
LoadDictionaryFromString(参考DictToTarget,strCollection);
我希望有足够的数据来帮助我。
再次感谢。

如果您能发布stringcollection数据的小样本,我们可以尝试一下,并让您知道。您传递给函数的输入是什么?我怀疑的是输入字符串没有;这些键是预定义类型的吗?您能对这些键执行类似switch语句的操作并安装正确的结构吗?为什么不使用使用已知的序列化程序(XmlSerializer、JavaScriptSerializer、SoapFormatter、BinaryFormatter、DataContractSeralizer、DataContractJsonSerializer、Json.Net、Protobuf等)试着创建一个自定义的吗?好吧,我按照前面解释的方法来做,因为我用最简单的方法将数据保存在c#上,保存到properties.settings。实际上,我不能保存字典,唯一可以接受的方法是stringCollection。如果你有其他方法,我很高兴听到……谢谢
    IDictionary<int, sampleStruct> DictToTarget=new IDictionary<int, sampleStruct>();

    // the Dictionary will be cleared and will contain the data as saved in the string collection
    // the stringCollection will contain the data above
    LoadDictionaryFromString<int, sampleStruct>(ref DictToTarget, strCollection);