Browse Source

Merge pull request #5894 from abpframework/maliming/ReflectionHelper-patch-3.3

Prevent ReflectionHelper's GetValueByPath method use null values.
pull/5911/head
Halil İbrahim Kalkan 6 years ago
committed by GitHub
parent
commit
8bb6dcd399
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      framework/src/Volo.Abp.Core/Volo/Abp/Reflection/ReflectionHelper.cs
  2. 49
      framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/ReflectionHelper_Tests.cs

16
framework/src/Volo.Abp.Core/Volo/Abp/Reflection/ReflectionHelper.cs

@ -124,7 +124,7 @@ namespace Volo.Abp.Reflection
? customAttributes.Concat(declaringTypeCustomAttributes).Distinct() ? customAttributes.Concat(declaringTypeCustomAttributes).Distinct()
: customAttributes; : customAttributes;
} }
/// <summary> /// <summary>
/// Gets value of a property by it's full path from given object /// Gets value of a property by it's full path from given object
/// </summary> /// </summary>
@ -134,7 +134,7 @@ namespace Volo.Abp.Reflection
var currentType = objectType; var currentType = objectType;
var objectPath = currentType.FullName; var objectPath = currentType.FullName;
var absolutePropertyPath = propertyPath; var absolutePropertyPath = propertyPath;
if (absolutePropertyPath.StartsWith(objectPath)) if (objectPath != null && absolutePropertyPath.StartsWith(objectPath))
{ {
absolutePropertyPath = absolutePropertyPath.Replace(objectPath + ".", ""); absolutePropertyPath = absolutePropertyPath.Replace(objectPath + ".", "");
} }
@ -142,8 +142,16 @@ namespace Volo.Abp.Reflection
foreach (var propertyName in absolutePropertyPath.Split('.')) foreach (var propertyName in absolutePropertyPath.Split('.'))
{ {
var property = currentType.GetProperty(propertyName); var property = currentType.GetProperty(propertyName);
value = property.GetValue(value, null); if (property != null)
currentType = property.PropertyType; {
value = property.GetValue(value, null);
currentType = property.PropertyType;
}
else
{
value = null;
break;
}
} }
return value; return value;

49
framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/ReflectionHelper_Tests.cs

@ -1,4 +1,5 @@
using System.Linq; using System;
using System.Linq;
using Shouldly; using Shouldly;
using Xunit; using Xunit;
@ -8,6 +9,50 @@ namespace Volo.Abp.Reflection
{ {
//TODO: ... //TODO: ...
class GetValueByPathTestClass
{
public string Name { get; set; }
public int Count { get; set; }
public DateTime Time { get; set; }
public GetValueByPathTestChildrenClass Children { get; set; }
}
class GetValueByPathTestChildrenClass
{
public string Name { get; set; }
public int Count { get; set; }
}
[Fact]
public void GetValueByPath_Test()
{
var value = new GetValueByPathTestClass
{
Name = "test",
Count = 8,
Time = DateTime.Parse("2020-01-01"),
Children = new GetValueByPathTestChildrenClass
{
Name = "test-children",
Count = 9,
}
};
ReflectionHelper.GetValueByPath(value, value.GetType(), "Name").ShouldBe("test");
ReflectionHelper.GetValueByPath(value, value.GetType(), "Volo.Abp.Reflection.ReflectionHelper_Tests+GetValueByPathTestClass.Name").ShouldBe("test");
ReflectionHelper.GetValueByPath(value, value.GetType(), "Count").ShouldBe(8);
ReflectionHelper.GetValueByPath(value, value.GetType(), "Time").ShouldBe(DateTime.Parse("2020-01-01"));
ReflectionHelper.GetValueByPath(value, value.GetType(), "Children.Name").ShouldBe("test-children");
ReflectionHelper.GetValueByPath(value, value.GetType(), "Children.Count").ShouldBe(9);
ReflectionHelper.GetValueByPath(value, value.GetType(), "Volo.Abp.Reflection.ReflectionHelper_Tests+GetValueByPathTestClass.Children.Name").ShouldBe("test-children");
ReflectionHelper.GetValueByPath(value, value.GetType(), "Children.NotExist").ShouldBeNull();
ReflectionHelper.GetValueByPath(value, value.GetType(), "NotExist").ShouldBeNull();
}
[Fact] [Fact]
public void GetPublicConstantsRecursively_Test() public void GetPublicConstantsRecursively_Test()
@ -99,4 +144,4 @@ namespace Volo.Abp.Reflection
} }
} }
} }

Loading…
Cancel
Save