Browse Source

Merge pull request #5445 from abpframework/maliming/guid-patch

Make `T To<T>`  extension method support string to GUID.
pull/5448/head
Halil İbrahim Kalkan 6 years ago
committed by GitHub
parent
commit
2f7240e28b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      framework/src/Volo.Abp.Core/System/AbpObjectExtensions.cs
  2. 6
      framework/test/Volo.Abp.Core.Tests/System/ObjectExtension_Test.cs

14
framework/src/Volo.Abp.Core/System/AbpObjectExtensions.cs

@ -1,4 +1,5 @@
using System.Globalization; using System.ComponentModel;
using System.Globalization;
using System.Linq; using System.Linq;
namespace System namespace System
@ -9,7 +10,7 @@ namespace System
public static class AbpObjectExtensions public static class AbpObjectExtensions
{ {
/// <summary> /// <summary>
/// Used to simplify and beautify casting an object to a type. /// Used to simplify and beautify casting an object to a type.
/// </summary> /// </summary>
/// <typeparam name="T">Type to be casted</typeparam> /// <typeparam name="T">Type to be casted</typeparam>
/// <param name="obj">Object to cast</param> /// <param name="obj">Object to cast</param>
@ -29,6 +30,11 @@ namespace System
public static T To<T>(this object obj) public static T To<T>(this object obj)
where T : struct where T : struct
{ {
if (typeof(T) == typeof(Guid))
{
return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(obj.ToString());
}
return (T)Convert.ChangeType(obj, typeof(T), CultureInfo.InvariantCulture); return (T)Convert.ChangeType(obj, typeof(T), CultureInfo.InvariantCulture);
} }
@ -42,7 +48,7 @@ namespace System
{ {
return list.Contains(item); return list.Contains(item);
} }
/// <summary> /// <summary>
/// Can be used to conditionally perform a function /// Can be used to conditionally perform a function
/// on an object and return the modified or the original object. /// on an object and return the modified or the original object.
@ -65,7 +71,7 @@ namespace System
return obj; return obj;
} }
/// <summary> /// <summary>
/// Can be used to conditionally perform an action /// Can be used to conditionally perform an action
/// on an object and return the original object. /// on an object and return the original object.

6
framework/test/Volo.Abp.Core.Tests/System/ObjectExtension_Test.cs

@ -35,6 +35,8 @@ namespace System
Assert.Throws<FormatException>(() => "test".To<bool>()); Assert.Throws<FormatException>(() => "test".To<bool>());
Assert.Throws<FormatException>(() => "test".To<int>()); Assert.Throws<FormatException>(() => "test".To<int>());
"2260AFEC-BBFD-42D4-A91A-DCB11E09B17F".To<Guid>().ShouldBeOfType<Guid>().ShouldBe(new Guid("2260afec-bbfd-42d4-a91a-dcb11e09b17f"));
} }
[Fact] [Fact]
@ -57,10 +59,10 @@ namespace System
public void If_Tests() public void If_Tests()
{ {
var value = 0; var value = 0;
value = value.If(true, v => v + 1); value = value.If(true, v => v + 1);
value.ShouldBe(1); value.ShouldBe(1);
value = value.If(false, v => v + 1); value = value.If(false, v => v + 1);
value.ShouldBe(1); value.ShouldBe(1);

Loading…
Cancel
Save