From eec13fa333d6cfb71abd0aac7b8cfcf833b70e88 Mon Sep 17 00:00:00 2001 From: Mladen Macanovic Date: Mon, 12 Oct 2020 10:57:09 +0200 Subject: [PATCH] New extension method to reset an object properties --- .../System/AbpObjectExtensions.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/framework/src/Volo.Abp.Core/System/AbpObjectExtensions.cs b/framework/src/Volo.Abp.Core/System/AbpObjectExtensions.cs index 958398e0c5..9542383983 100644 --- a/framework/src/Volo.Abp.Core/System/AbpObjectExtensions.cs +++ b/framework/src/Volo.Abp.Core/System/AbpObjectExtensions.cs @@ -2,6 +2,7 @@ using System.ComponentModel; using System.Globalization; using System.Linq; +using System.Reflection; namespace System { @@ -105,5 +106,36 @@ namespace System return obj; } + + /// + /// Resets all fields of a given object to their default values. + /// + /// Type of the object + /// An object + /// Alternative custom method to reset the values. + /// Returns the original object. + public static T ResetState(this T obj, Action customReset = null) + { + if (customReset != null) + { + customReset(obj); + } + else + { + var properties = typeof(T).GetProperties(); + + foreach (var property in properties) + { + if (!property.CanWrite) + { + continue; + } + + property.SetValue(obj, null); + } + } + + return obj; + } } }