修复config<T> 配置文件List<T> 类型字段在读时出现的超出索引界限问题郗云飞 authored at 2021-03-16 22:57:26
diff --git a/NewLife.Core/Configuration/ConfigHelper.cs b/NewLife.Core/Configuration/ConfigHelper.cs
index 425d420..ac2b159 100644
--- a/NewLife.Core/Configuration/ConfigHelper.cs
+++ b/NewLife.Core/Configuration/ConfigHelper.cs
@@ -211,8 +211,16 @@ namespace NewLife.Configuration
for (var i = 0; i < section.Childs.Count; i++)
{
var val = elementType.CreateInstance();
- MapTo(section.Childs[i], val, provider);
- list[i] = val;
+ if (elementType.GetTypeCode() != TypeCode.Object)
+ {
+ val = section.Childs[i].Value;
+ }
+ else
+ {
+ MapTo(section.Childs[i], val, provider);
+ //list[i] = val;
+ }
+ list.Add(val);
}
}
diff --git a/Test/ConfigTest.cs b/Test/ConfigTest.cs
new file mode 100644
index 0000000..fea95f4
--- /dev/null
+++ b/Test/ConfigTest.cs
@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using NewLife.Configuration;
+
+namespace Test
+{
+ public class ConfigTest : Config<ConfigTest>
+ {
+ public List<String> Names { get; set; }
+
+ public String Sex { get; set; }
+
+ public List<XYF> xyf { get; set; }
+ }
+
+ public class XYF
+ {
+ public String name { get; set; }
+
+ //public List<String> gradu { get; set; }
+ }
+}
diff --git a/Test/Program.cs b/Test/Program.cs
index 4f841b3..ab0f805 100644
--- a/Test/Program.cs
+++ b/Test/Program.cs
@@ -69,7 +69,7 @@ namespace Test
try
{
#endif
- Test1();
+ Test1();
#if !DEBUG
}
catch (Exception ex)
@@ -897,5 +897,29 @@ namespace Test
Console.WriteLine(str);
Console.WriteLine(DAL.ConnStrs["MySQL.AppSettings.default"]);
}
+
+ /// <summary>测试config文件的写入</summary>
+ private static void TestWriteConfig()
+ {
+ ConfigTest.Current.Names = new List<string> { "1", "2" };
+ ConfigTest.Current.Sex = "1";
+ ConfigTest.Current.xyf = new List<XYF>() { new XYF() { name = "123" }, new XYF() { name = "321" } };
+ ConfigTest.Current.Save();
+
+ //Class1.Current.Names = "123";
+ //Class1.Current.Save();
+
+ //Class1.Provider = XmlConfig;
+
+
+ }
+
+ /// <summary>测试config文件的读取</summary>
+ private static void TestReadConfig()
+ {
+ var z = ConfigTest.Current.Names;
+ var x = ConfigTest.Current.Sex;
+ var y = ConfigTest.Current.xyf;
+ }
}
}
\ No newline at end of file
diff --git "a/X\347\273\204\344\273\266.sln" "b/X\347\273\204\344\273\266.sln"
index 45ad77b..86334a8 100644
--- "a/X\347\273\204\344\273\266.sln"
+++ "b/X\347\273\204\344\273\266.sln"
@@ -37,7 +37,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NewLife.WinCore", "NewLife.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XCodeTool", "XCodeTool\XCodeTool.csproj", "{ADAD9BA3-5AB4-478A-BB51-3E144D410F59}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewLife.Security", "NewLife.Security\NewLife.Security.csproj", "{13D57F4A-2A7C-46FE-8472-DD3BA67F939D}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NewLife.Security", "NewLife.Security\NewLife.Security.csproj", "{13D57F4A-2A7C-46FE-8472-DD3BA67F939D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution