Skip to content
This repository was archived by the owner on Aug 2, 2020. It is now read-only.

Commit fa7e4bc

Browse files
authored
Merge pull request #160 from Jie2GG/Test
修复 IniConfig 的 bug
2 parents 5623308 + 85fbc7c commit fa7e4bc

File tree

6 files changed

+99
-12
lines changed

6 files changed

+99
-12
lines changed

Native.Core/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
3333
//通过使用 "*",如下所示:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion ("4.2.1.0429")]
36-
[assembly: AssemblyFileVersion ("4.2.1.0429")]
35+
[assembly: AssemblyVersion ("4.2.2.0501")]
36+
[assembly: AssemblyFileVersion ("4.2.2.0501")]

Native.Tool/IniConfig/IniConfig.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,8 @@ public object GetObject (string sectionKey, Type t)
357357
IniKeyAttribute keyAttribute = properties[i].GetCustomAttribute<IniKeyAttribute> ();
358358
if ((keyAttribute != null && keyAttribute.KeyName.Equals (item.Key)) || properties[i].Name.Equals (item.Key))
359359
{
360-
MethodInfo method = properties[i].GetSetMethod (true);
361-
if (method.GetParameters ().Count () == 0)
362-
{
363-
method.Invoke (instance, new object[] { item.Value });
364-
}
365-
360+
object value = Convert.ChangeType (item.Value, properties[i].PropertyType);
361+
properties[i].GetSetMethod (true).Invoke (instance, new object[] { value });
366362
properties.RemoveAt (i);
367363
break;
368364
}

Native.Tool/IniConfig/Linq/ISection.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,67 @@ public ISection (string sectionName, IDictionary<string, IValue> dictionary, ICo
9090

9191
#region --公开方法--
9292
/// <summary>
93+
/// 获取与指定的键关联的值, 转换为指定的类型. 若键不存在将返回 T 的默认值
94+
/// </summary>
95+
/// <typeparam name="T">转换目标值的类型</typeparam>
96+
/// <param name="key">要获取其值的键</param>
97+
/// <exception cref="InvalidOperationException">当前类型不是泛型类型。 也就是说,<see cref="Type.IsGenericType"/> 返回 <see langword="false"/></exception>
98+
/// <exception cref="NotSupportedException">基类不支持调用的方法。 派生类必须提供一个实现</exception>
99+
/// <exception cref="InvalidCastException">不支持此转换</exception>
100+
/// <exception cref="FormatException">转换的目标格式不是 provider 可识别的 T 的格式</exception>
101+
/// <exception cref="OverflowException">原始值表示不在 T 的范围内的数字</exception>
102+
/// <returns>获取关联的值并转换为目标类型</returns>
103+
public T GetValue<T>(string key)
104+
{
105+
return GetValueOrDefault<T> (key, default (T));
106+
}
107+
/// <summary>
108+
/// 获取与指定的键关联的值, 转换为指定的类型. 若键不存在将返回 defaultValue
109+
/// </summary>
110+
/// <typeparam name="T">转换目标值的类型</typeparam>
111+
/// <param name="key">要获取其值的键</param>
112+
/// <param name="defaultValue">当键不存在时返回的默认值</param>
113+
/// <exception cref="InvalidOperationException">当前类型不是泛型类型。 也就是说,<see cref="Type.IsGenericType"/> 返回 <see langword="false"/></exception>
114+
/// <exception cref="NotSupportedException">基类不支持调用的方法。 派生类必须提供一个实现</exception>
115+
/// <exception cref="InvalidCastException">不支持此转换</exception>
116+
/// <exception cref="FormatException">转换的目标格式不是 provider 可识别的 T 的格式</exception>
117+
/// <exception cref="OverflowException">原始值表示不在 T 的范围内的数字</exception>
118+
/// <returns>获取关联的值并转换为目标类型</returns>
119+
public T GetValueOrDefault<T> (string key, T defaultValue)
120+
{
121+
if (!this.ContainsKey (key))
122+
{
123+
return defaultValue;
124+
}
125+
126+
IValue iValue = this[key];
127+
if (iValue is T)
128+
{
129+
T result = (T)(iValue.Value);
130+
if (typeof(T) != typeof(IComparable) && typeof(T) != typeof(IFormattable))
131+
{
132+
return result;
133+
}
134+
}
135+
136+
object objValue = iValue.Value;
137+
if (objValue is T)
138+
{
139+
return (T)objValue;
140+
}
141+
142+
Type type = typeof (T);
143+
if (ReflectionUtils.IsNullableType(type))
144+
{
145+
if (objValue == null)
146+
{
147+
return defaultValue;
148+
}
149+
type = Nullable.GetUnderlyingType (type);
150+
}
151+
return (T)Convert.ChangeType (objValue, type, CultureInfo.InvariantCulture);
152+
}
153+
/// <summary>
93154
/// 确定此实例是否与另一个指定的 <see cref="ISection"/> 对象具有相同的值
94155
/// </summary>
95156
/// <param name="other">要与实例进行比较的 <see cref="ISection"/></param>

Native.Tool/IniConfig/Linq/IValue.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,31 @@ public object ToType (Type conversionType)
702702
/// <returns><see cref="object"/> 类型的实例 conversionType 其值等效于此实例的值</returns>
703703
public object ToType (Type conversionType, IFormatProvider provider)
704704
{
705+
if (conversionType.Equals (typeof (byte[])))
706+
{
707+
return (byte[])this;
708+
}
709+
710+
if (conversionType.Equals (typeof (DateTimeOffset)))
711+
{
712+
return (DateTimeOffset)this;
713+
}
714+
715+
if (conversionType.Equals (typeof (Guid)))
716+
{
717+
return (Guid)this;
718+
}
719+
720+
if (conversionType.Equals (typeof (Uri)))
721+
{
722+
return (Uri)this;
723+
}
724+
725+
if (conversionType.Equals (typeof (TimeSpan)))
726+
{
727+
return (TimeSpan)this;
728+
}
729+
705730
return Convert.ChangeType (this._value, conversionType, provider);
706731
}
707732
#endregion
@@ -1342,7 +1367,7 @@ public static implicit operator byte (IValue value)
13421367
public static implicit operator char (IValue value)
13431368
{
13441369
object ivalue = value._value;
1345-
if (ivalue != null || !IValue.IsConvert (value._valueType, IValue.ConvertCharTypes, false))
1370+
if (ivalue == null || !IValue.IsConvert (value._valueType, IValue.ConvertCharTypes, false))
13461371
{
13471372
throw new ArgumentException (string.Format ("无法将 {0} 转换为 Char", value._value.GetType ().Name));
13481373
}
@@ -1718,7 +1743,7 @@ public static implicit operator Uri (IValue value)
17181743
Uri result = ivalue as Uri;
17191744
if (result == null)
17201745
{
1721-
return new Uri (Convert.ToString (ivalue, CultureInfo.InvariantCulture));
1746+
return new Uri (Convert.ToString (ivalue, CultureInfo.InvariantCulture), UriKind.RelativeOrAbsolute);
17221747
}
17231748
return result;
17241749
}

Native.Tool/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
3333
//通过使用 "*",如下所示:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion ("3.3.1.0429")]
36-
[assembly: AssemblyFileVersion ("3.3.1.0429")]
35+
[assembly: AssemblyVersion ("3.3.2.0501")]
36+
[assembly: AssemblyFileVersion ("3.3.2.0501")]

UPDATE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## Native.SDK 更新日志
22

3+
#### 2020年05月01日 版本: V4.2.2.0501
4+
5+
1. 修复 IniConfig 反序列化时设置值失败
6+
2. 修复 IValue 针对某些类型的数据转换问题
7+
38
#### 2020年04月29日 版本: V4.2.1.0429
49

510
1. 修复 QQMessage 类 CQCode 解析问题

0 commit comments

Comments
 (0)