mirror of
https://github.com/Stone-Red-Code/CuteUtils.git
synced 2026-09-04 00:46:11 +02:00
Fix DynDto
This commit is contained in:
@@ -5,10 +5,10 @@ using System.Dynamic;
|
||||
namespace CuteUtils.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class UnitTest1
|
||||
public class DynDtoTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void TestMethod1()
|
||||
public void Convert_ToDto_ReturnsExpandoObject()
|
||||
{
|
||||
TestModel testModel = new()
|
||||
{
|
||||
@@ -27,7 +27,7 @@ public class UnitTest1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestMethod2()
|
||||
public void Convert_ToDto_ReturnsGeneric()
|
||||
{
|
||||
TestModel testModel = new()
|
||||
{
|
||||
@@ -40,9 +40,9 @@ public class UnitTest1
|
||||
|
||||
Assert.IsNotNull(dto);
|
||||
|
||||
Assert.IsTrue(dto.Id != 0);
|
||||
Assert.IsTrue(dto.Name is not null);
|
||||
Assert.IsTrue(dto.Value is not null);
|
||||
Assert.IsTrue(dto.Id == 0);
|
||||
Assert.IsTrue(!string.IsNullOrWhiteSpace(dto.Name));
|
||||
Assert.IsTrue(dto.Value is not (0, ""));
|
||||
}
|
||||
|
||||
private class TestModel
|
||||
@@ -52,14 +52,14 @@ public class UnitTest1
|
||||
[DynDtoName(nameof(Name))]
|
||||
public required string Name { get; set; }
|
||||
|
||||
//[DynDtoName(nameof(Value))]
|
||||
[DynDtoName(nameof(Value))]
|
||||
public required object Value { get; set; }
|
||||
}
|
||||
|
||||
private class TestDto
|
||||
{
|
||||
public int Id { get; set; } = 0;
|
||||
public int Id { get; init; } = 0;
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public object Value { get; set; } = (15, "test");
|
||||
public object Value { get; init; } = (0, string.Empty);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace CuteUtils.Reflection;
|
||||
|
||||
public static class DynDto
|
||||
{
|
||||
public static ExpandoObject ToDto(this object data)
|
||||
@@ -25,8 +26,10 @@ public static class DynDto
|
||||
|
||||
public static T ToDto<T>(this object data, T dto)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(dto);
|
||||
|
||||
PropertyInfo[] dataProperties = data.GetType().GetProperties();
|
||||
PropertyInfo[] dtoProperties = data.GetType().GetProperties();
|
||||
PropertyInfo[] dtoProperties = dto.GetType().GetProperties();
|
||||
|
||||
foreach (PropertyInfo dataProperty in dataProperties)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user