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

Commit

Permalink
Merge pull request #160 from Jie2GG/Test
Browse files Browse the repository at this point in the history
修复 IniConfig 的 bug
  • Loading branch information
Jie2GG authored May 1, 2020
2 parents 5623308 + 85fbc7c commit fa7e4bc
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Native.Core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion ("4.2.1.0429")]
[assembly: AssemblyFileVersion ("4.2.1.0429")]
[assembly: AssemblyVersion ("4.2.2.0501")]
[assembly: AssemblyFileVersion ("4.2.2.0501")]
8 changes: 2 additions & 6 deletions Native.Tool/IniConfig/IniConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,8 @@ public object GetObject (string sectionKey, Type t)
IniKeyAttribute keyAttribute = properties[i].GetCustomAttribute<IniKeyAttribute> ();
if ((keyAttribute != null && keyAttribute.KeyName.Equals (item.Key)) || properties[i].Name.Equals (item.Key))
{
MethodInfo method = properties[i].GetSetMethod (true);
if (method.GetParameters ().Count () == 0)
{
method.Invoke (instance, new object[] { item.Value });
}

object value = Convert.ChangeType (item.Value, properties[i].PropertyType);
properties[i].GetSetMethod (true).Invoke (instance, new object[] { value });
properties.RemoveAt (i);
break;
}
Expand Down
61 changes: 61 additions & 0 deletions Native.Tool/IniConfig/Linq/ISection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,67 @@ public ISection (string sectionName, IDictionary<string, IValue> dictionary, ICo

#region --公开方法--
/// <summary>
/// 获取与指定的键关联的值, 转换为指定的类型. 若键不存在将返回 T 的默认值
/// </summary>
/// <typeparam name="T">转换目标值的类型</typeparam>
/// <param name="key">要获取其值的键</param>
/// <exception cref="InvalidOperationException">当前类型不是泛型类型。 也就是说,<see cref="Type.IsGenericType"/> 返回 <see langword="false"/></exception>
/// <exception cref="NotSupportedException">基类不支持调用的方法。 派生类必须提供一个实现</exception>
/// <exception cref="InvalidCastException">不支持此转换</exception>
/// <exception cref="FormatException">转换的目标格式不是 provider 可识别的 T 的格式</exception>
/// <exception cref="OverflowException">原始值表示不在 T 的范围内的数字</exception>
/// <returns>获取关联的值并转换为目标类型</returns>
public T GetValue<T>(string key)
{
return GetValueOrDefault<T> (key, default (T));
}
/// <summary>
/// 获取与指定的键关联的值, 转换为指定的类型. 若键不存在将返回 defaultValue
/// </summary>
/// <typeparam name="T">转换目标值的类型</typeparam>
/// <param name="key">要获取其值的键</param>
/// <param name="defaultValue">当键不存在时返回的默认值</param>
/// <exception cref="InvalidOperationException">当前类型不是泛型类型。 也就是说,<see cref="Type.IsGenericType"/> 返回 <see langword="false"/></exception>
/// <exception cref="NotSupportedException">基类不支持调用的方法。 派生类必须提供一个实现</exception>
/// <exception cref="InvalidCastException">不支持此转换</exception>
/// <exception cref="FormatException">转换的目标格式不是 provider 可识别的 T 的格式</exception>
/// <exception cref="OverflowException">原始值表示不在 T 的范围内的数字</exception>
/// <returns>获取关联的值并转换为目标类型</returns>
public T GetValueOrDefault<T> (string key, T defaultValue)
{
if (!this.ContainsKey (key))
{
return defaultValue;
}

IValue iValue = this[key];
if (iValue is T)
{
T result = (T)(iValue.Value);
if (typeof(T) != typeof(IComparable) && typeof(T) != typeof(IFormattable))
{
return result;
}
}

object objValue = iValue.Value;
if (objValue is T)
{
return (T)objValue;
}

Type type = typeof (T);
if (ReflectionUtils.IsNullableType(type))
{
if (objValue == null)
{
return defaultValue;
}
type = Nullable.GetUnderlyingType (type);
}
return (T)Convert.ChangeType (objValue, type, CultureInfo.InvariantCulture);
}
/// <summary>
/// 确定此实例是否与另一个指定的 <see cref="ISection"/> 对象具有相同的值
/// </summary>
/// <param name="other">要与实例进行比较的 <see cref="ISection"/></param>
Expand Down
29 changes: 27 additions & 2 deletions Native.Tool/IniConfig/Linq/IValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,31 @@ public object ToType (Type conversionType)
/// <returns><see cref="object"/> 类型的实例 conversionType 其值等效于此实例的值</returns>
public object ToType (Type conversionType, IFormatProvider provider)
{
if (conversionType.Equals (typeof (byte[])))
{
return (byte[])this;
}

if (conversionType.Equals (typeof (DateTimeOffset)))
{
return (DateTimeOffset)this;
}

if (conversionType.Equals (typeof (Guid)))
{
return (Guid)this;
}

if (conversionType.Equals (typeof (Uri)))
{
return (Uri)this;
}

if (conversionType.Equals (typeof (TimeSpan)))
{
return (TimeSpan)this;
}

return Convert.ChangeType (this._value, conversionType, provider);
}
#endregion
Expand Down Expand Up @@ -1342,7 +1367,7 @@ public static implicit operator byte (IValue value)
public static implicit operator char (IValue value)
{
object ivalue = value._value;
if (ivalue != null || !IValue.IsConvert (value._valueType, IValue.ConvertCharTypes, false))
if (ivalue == null || !IValue.IsConvert (value._valueType, IValue.ConvertCharTypes, false))
{
throw new ArgumentException (string.Format ("无法将 {0} 转换为 Char", value._value.GetType ().Name));
}
Expand Down Expand Up @@ -1718,7 +1743,7 @@ public static implicit operator Uri (IValue value)
Uri result = ivalue as Uri;
if (result == null)
{
return new Uri (Convert.ToString (ivalue, CultureInfo.InvariantCulture));
return new Uri (Convert.ToString (ivalue, CultureInfo.InvariantCulture), UriKind.RelativeOrAbsolute);
}
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions Native.Tool/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion ("3.3.1.0429")]
[assembly: AssemblyFileVersion ("3.3.1.0429")]
[assembly: AssemblyVersion ("3.3.2.0501")]
[assembly: AssemblyFileVersion ("3.3.2.0501")]
5 changes: 5 additions & 0 deletions UPDATE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Native.SDK 更新日志

#### 2020年05月01日 版本: V4.2.2.0501

1. 修复 IniConfig 反序列化时设置值失败
2. 修复 IValue 针对某些类型的数据转换问题

#### 2020年04月29日 版本: V4.2.1.0429

1. 修复 QQMessage 类 CQCode 解析问题
Expand Down

0 comments on commit fa7e4bc

Please sign in to comment.