diff --git a/src/CuteUtils.Tests/CuteUtils.Tests.csproj b/src/CuteUtils.Tests/CuteUtils.Tests.csproj new file mode 100644 index 0000000..95dadc8 --- /dev/null +++ b/src/CuteUtils.Tests/CuteUtils.Tests.csproj @@ -0,0 +1,27 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + diff --git a/src/CuteUtils.Tests/UnitTest1.cs b/src/CuteUtils.Tests/UnitTest1.cs new file mode 100644 index 0000000..9207c72 --- /dev/null +++ b/src/CuteUtils.Tests/UnitTest1.cs @@ -0,0 +1,65 @@ +using CuteUtils.Reflection; + +using System.Dynamic; + +namespace CuteUtils.Tests; + +[TestClass] +public class UnitTest1 +{ + [TestMethod] + public void TestMethod1() + { + TestModel testModel = new() + { + Id = 13, + Name = "test", + Value = (15, "test"), + }; + + ExpandoObject dto = testModel.ToDto(); + + Assert.IsNotNull(dto); + + Assert.IsFalse(dto.Any(p => p.Key == "Id")); + Assert.IsTrue(dto.Any(p => p.Key == "Name")); + Assert.IsTrue(dto.Any(p => p.Key == "Value")); + } + + [TestMethod] + public void TestMethod2() + { + TestModel testModel = new() + { + Id = 13, + Name = "test", + Value = (15, "test"), + }; + + TestDto dto = testModel.ToDto(); + + Assert.IsNotNull(dto); + + Assert.IsTrue(dto.Id != 0); + Assert.IsTrue(dto.Name is not null); + Assert.IsTrue(dto.Value is not null); + } + + private class TestModel + { + public required int Id { get; set; } + + [DynDtoName(nameof(Name))] + public required string Name { get; set; } + + //[DynDtoName(nameof(Value))] + public required object Value { get; set; } + } + + private class TestDto + { + public int Id { get; set; } = 0; + public string Name { get; set; } = string.Empty; + public object Value { get; set; } = (15, "test"); + } +} \ No newline at end of file diff --git a/src/CuteUtils.sln b/src/CuteUtils.sln index 5914960..53b2187 100644 --- a/src/CuteUtils.sln +++ b/src/CuteUtils.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.9.34622.214 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CuteUtils", "CuteUtils\CuteUtils.csproj", "{D640F3B6-2B09-496D-88D6-3CF57559845D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CuteUtils", "CuteUtils\CuteUtils.csproj", "{D640F3B6-2B09-496D-88D6-3CF57559845D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CuteUtils.Tests", "CuteUtils.Tests\CuteUtils.Tests.csproj", "{1468822C-92E4-4C44-93F6-DE71930F7C96}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {D640F3B6-2B09-496D-88D6-3CF57559845D}.Debug|Any CPU.Build.0 = Debug|Any CPU {D640F3B6-2B09-496D-88D6-3CF57559845D}.Release|Any CPU.ActiveCfg = Release|Any CPU {D640F3B6-2B09-496D-88D6-3CF57559845D}.Release|Any CPU.Build.0 = Release|Any CPU + {1468822C-92E4-4C44-93F6-DE71930F7C96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1468822C-92E4-4C44-93F6-DE71930F7C96}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1468822C-92E4-4C44-93F6-DE71930F7C96}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1468822C-92E4-4C44-93F6-DE71930F7C96}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/CuteUtils/BoolExtentions.cs b/src/CuteUtils/BoolExtentions.cs index 93a9d24..71e8819 100644 --- a/src/CuteUtils/BoolExtentions.cs +++ b/src/CuteUtils/BoolExtentions.cs @@ -8,26 +8,26 @@ public static class BoolExt /// /// Sets value to true if input is true. If input is false the value will not change. /// - /// + /// /// - public static void OneWayTrue(this ref bool bol, bool input) + public static void OneWayTrue(this ref bool value, bool input) { - if (!bol && input) + if (!value && input) { - bol = true; + value = true; } } /// /// Sets value to false if input is false. If input is true the value will not change. /// - /// + /// /// - public static void OneWayFalse(this ref bool bol, bool input) + public static void OneWayFalse(this ref bool value, bool input) { - if (bol && !input) + if (value && !input) { - bol = false; + value = false; } } diff --git a/src/CuteUtils/CuteUtils.csproj b/src/CuteUtils/CuteUtils.csproj index fa71b7a..b4ef780 100644 --- a/src/CuteUtils/CuteUtils.csproj +++ b/src/CuteUtils/CuteUtils.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/src/CuteUtils/Logging/LogConfig.cs b/src/CuteUtils/Logging/LogConfig.cs index 67306e6..cb7276b 100644 --- a/src/CuteUtils/Logging/LogConfig.cs +++ b/src/CuteUtils/Logging/LogConfig.cs @@ -1,8 +1,4 @@ -using Stone_Red_C_Sharp_Utilities.Logging; - -using Stone_Red_Utilities.Logging; - -namespace CuteUtils.Logging; +namespace CuteUtils.Logging; /// /// Logging configuration. diff --git a/src/CuteUtils/Logging/LogFormatBuilder.cs b/src/CuteUtils/Logging/LogFormatBuilder.cs index cfb0cd3..5ae36b1 100644 --- a/src/CuteUtils/Logging/LogFormatBuilder.cs +++ b/src/CuteUtils/Logging/LogFormatBuilder.cs @@ -1,6 +1,4 @@ -using Stone_Red_C_Sharp_Utilities.Logging; - -using System.Text; +using System.Text; namespace CuteUtils.Logging; diff --git a/src/CuteUtils/Logging/Logger.cs b/src/CuteUtils/Logging/Logger.cs index 7ff58f9..73f13b3 100644 --- a/src/CuteUtils/Logging/Logger.cs +++ b/src/CuteUtils/Logging/Logger.cs @@ -1,6 +1,4 @@ -using Stone_Red_C_Sharp_Utilities; - -using System.Diagnostics; +using System.Diagnostics; using System.Runtime.CompilerServices; namespace CuteUtils.Logging; diff --git a/src/CuteUtils/Reflection/DynDto.cs b/src/CuteUtils/Reflection/DynDto.cs new file mode 100644 index 0000000..606ce6a --- /dev/null +++ b/src/CuteUtils/Reflection/DynDto.cs @@ -0,0 +1,105 @@ +using System.Dynamic; +using System.Reflection; + +namespace CuteUtils.Reflection; +public static class DynDto +{ + public static ExpandoObject ToDto(this object data) + { + ExpandoObject dto = new ExpandoObject(); + + PropertyInfo[] properties = data.GetType().GetProperties(); + + foreach (PropertyInfo property in properties) + { + DynDtoNameAttribute? dynDtoNameAttribute = property.GetCustomAttribute(); + + if (dynDtoNameAttribute is not null) + { + _ = dto.TryAdd(dynDtoNameAttribute.Name, property.GetValue(data)); + } + } + + return dto; + } + + public static T ToDto(this object data, T dto) + { + PropertyInfo[] dataProperties = data.GetType().GetProperties(); + PropertyInfo[] dtoProperties = data.GetType().GetProperties(); + + foreach (PropertyInfo dataProperty in dataProperties) + { + DynDtoNameAttribute? dynDtoNameAttribute = dataProperty.GetCustomAttribute(); + PropertyInfo? dtoPropertyInfo = Array.Find(dtoProperties, p => p.Name == dynDtoNameAttribute?.Name); + + if (dynDtoNameAttribute is not null && dtoPropertyInfo is not null) + { + object? value = dataProperty.GetValue(data); + dtoPropertyInfo.SetValue(dto, value); + } + } + + return dto; + } + + public static T ToDto(this object data) where T : new() + { + return ToDto(data, new T()); + } + + public static T FromDto(this object dto, T data) + { + ArgumentNullException.ThrowIfNull(data); + + PropertyInfo[] dataProperties = data.GetType().GetProperties(); + PropertyInfo[] dtoProperties = dto.GetType().GetProperties(); + + foreach (PropertyInfo property in dataProperties) + { + DynDtoNameAttribute? dynDtoNameAttribute = property.GetCustomAttribute(); + if (dynDtoNameAttribute is not null) + { + PropertyInfo? propertyInfo = Array.Find(dtoProperties, x => x.Name == dynDtoNameAttribute.Name); + + if (propertyInfo is not null) + { + property.SetValue(data, propertyInfo.GetValue(dto)); + } + } + } + + return data; + } + + public static T FromDto(this ExpandoObject dto, T data) + { + ArgumentNullException.ThrowIfNull(dto); + ArgumentNullException.ThrowIfNull(data); + + IDictionary dtoProperties = dto!; + + PropertyInfo[] dataProperties = data.GetType().GetProperties(); + + foreach (PropertyInfo property in dataProperties) + { + DynDtoNameAttribute? dynDtoNameAttribute = property.GetCustomAttribute(); + if (dynDtoNameAttribute is not null && dtoProperties.TryGetValue(dynDtoNameAttribute.Name, out object? value)) + { + property.SetValue(data, value); + } + } + + return data; + } + + public static T FromDto(this object dto) where T : new() + { + return FromDto(dto, new T()); + } + + public static T FromDto(this ExpandoObject dto) where T : new() + { + return FromDto(dto, new T()); + } +} diff --git a/src/CuteUtils/Reflection/DynDtoNameAttribute.cs b/src/CuteUtils/Reflection/DynDtoNameAttribute.cs new file mode 100644 index 0000000..475350f --- /dev/null +++ b/src/CuteUtils/Reflection/DynDtoNameAttribute.cs @@ -0,0 +1,6 @@ +namespace CuteUtils.Reflection; +[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] +public class DynDtoNameAttribute(string name) : Attribute +{ + public string Name { get; set; } = name; +} diff --git a/src/CuteUtils/Reflection.cs b/src/CuteUtils/Reflection/ReflectionExtentions.cs similarity index 96% rename from src/CuteUtils/Reflection.cs rename to src/CuteUtils/Reflection/ReflectionExtentions.cs index a0ab95d..8459a45 100644 --- a/src/CuteUtils/Reflection.cs +++ b/src/CuteUtils/Reflection/ReflectionExtentions.cs @@ -1,11 +1,11 @@ using System.Reflection; -namespace CuteUtils; +namespace CuteUtils.Reflection; /// /// Reflection class /// -public static class Reflection +public static class ReflectionExtentions { /// /// Copies all properties of an object to a new one.