From 068b57b0573528877238226f85c7109c9b5bd83f Mon Sep 17 00:00:00 2001 From: RzR <108324929+I-RzR-I@users.noreply.github.com> Date: Wed, 29 May 2024 23:07:43 +0300 Subject: [PATCH] Adjust validation in ObjectExtensions. --- .../DataTypeExtensions/ObjectExtensions.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs index dc9b4e3..d076645 100644 --- a/src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs +++ b/src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs @@ -86,6 +86,9 @@ public static bool TryCast(this object obj, out T result) /// The xml document public static void GetXmlParams(this object obj, ref XmlDocument xmlDocument) { + if (obj.IsNull()) + throw new ArgumentNullException(nameof(obj)); + foreach (var prop in obj.GetType().GetProperties()) { var xmlNode = xmlDocument.SelectSingleNode("//" + prop.Name); @@ -103,6 +106,11 @@ public static void GetXmlParams(this object obj, ref XmlDocument xmlDocument) /// public static void CopyPropertiesTo(this object fromObject, object toObject) { + if (fromObject.IsNull()) + throw new ArgumentNullException(nameof(fromObject)); + if (toObject.IsNull()) + throw new ArgumentNullException(nameof(toObject)); + var toObjectProperties = toObject.GetType().GetProperties(); foreach (var propTo in toObjectProperties) { @@ -240,6 +248,8 @@ public static T To(this object source) [CodeSource("https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractserializer?view=netstandard-2.0", "", "MS", "2022-12-28", "Reference source")] public static string SerializeToString(this object obj) { + if (obj.IsNull()) return null; + using var memoryStream = new MemoryStream(); using var reader = new StreamReader(memoryStream); var serializer = new DataContractSerializer(obj.GetType());