From 081d332a0494d309c2762f76f9dc5032a673ea0d Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Sun, 7 Jul 2019 15:05:28 +0300 Subject: Removed Object extension methods from global namespace. --- .../ExtensionMethods/ObjectExtensions.cs | 333 +++++++++++---------- 1 file changed, 168 insertions(+), 165 deletions(-) (limited to 'Software/Visual_Studio/Tango.Core/ExtensionMethods') diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs index ce907b57b..b5e430829 100644 --- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs +++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs @@ -10,226 +10,229 @@ using System.Threading.Tasks; using Tango.Core.Json; using Tango.Serialization; -/// -/// Contains extension methods. -/// -public static class ObjectExtensions +namespace Tango.Core.ExtensionMethods { - private static bool _jsonSettingsSet; - /// - /// Performs shallow cloning of the object excluding generic properties.. + /// Contains extension methods. /// - /// - /// The object. - /// - public static T ShallowClone(this T obj) + public static class ObjectExtensions { - var cloned = Activator.CreateInstance(); - - foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.SetMethod != null)) + private static bool _jsonSettingsSet; + + /// + /// Performs shallow cloning of the object excluding generic properties.. + /// + /// + /// The object. + /// + public static T ShallowClone(this T obj) { - if (!prop.PropertyType.IsGenericType) + var cloned = Activator.CreateInstance(); + + foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.SetMethod != null)) { - prop.SetValue(cloned, prop.GetValue(obj)); + if (!prop.PropertyType.IsGenericType) + { + prop.SetValue(cloned, prop.GetValue(obj)); + } } - } - return cloned; - } + return cloned; + } - /// - /// Performs a shallow mapping to the specified object excluding generic properties. - /// - /// - /// The source. - /// The destination object. - public static void ShallowCopyTo(this T source, T destination) - { - foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.SetMethod != null)) + /// + /// Performs a shallow mapping to the specified object excluding generic properties. + /// + /// + /// The source. + /// The destination object. + public static void ShallowCopyTo(this T source, T destination) { - if (!prop.PropertyType.IsGenericType) + foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.SetMethod != null)) { - prop.SetValue(destination, prop.GetValue(source)); + if (!prop.PropertyType.IsGenericType) + { + prop.SetValue(destination, prop.GetValue(source)); + } } } - } - /// - /// Maps the object properties values to the destination object. - /// - /// The source. - /// The destination. - public static void MapPrimitivesTo(this object source, object destination) - { - foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive)) + /// + /// Maps the object properties values to the destination object. + /// + /// The source. + /// The destination. + public static void MapPrimitivesTo(this object source, object destination) { - var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance); - - if (desProp != null && desProp.PropertyType.IsPrimitive && desProp.SetMethod != null) + foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive)) { - desProp.SetValue(destination, prop.GetValue(source)); + var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance); + + if (desProp != null && desProp.PropertyType.IsPrimitive && desProp.SetMethod != null) + { + desProp.SetValue(destination, prop.GetValue(source)); + } } } - } - /// - /// Maps the object properties values to the destination object including strings and without assigning null string values from the source. - /// - /// The source. - /// The destination. - public static void MapPrimitivesWithStringsNoNullsTo(this object source, object destination) - { - foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive || x.PropertyType == typeof(String))) + /// + /// Maps the object properties values to the destination object including strings and without assigning null string values from the source. + /// + /// The source. + /// The destination. + public static void MapPrimitivesWithStringsNoNullsTo(this object source, object destination) { - var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance); - - if (desProp != null && (desProp.PropertyType.IsPrimitive || desProp.PropertyType == typeof(String)) && desProp.SetMethod != null) + foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive || x.PropertyType == typeof(String))) { - var value = prop.GetValue(source); + var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance); - if (desProp.PropertyType != typeof(String) || !String.IsNullOrEmpty(value as String)) + if (desProp != null && (desProp.PropertyType.IsPrimitive || desProp.PropertyType == typeof(String)) && desProp.SetMethod != null) { - desProp.SetValue(destination, value); + var value = prop.GetValue(source); + + if (desProp.PropertyType != typeof(String) || !String.IsNullOrEmpty(value as String)) + { + desProp.SetValue(destination, value); + } } } } - } - /// - /// Maps the object properties values to the destination object including strings and without assigning null string values from the source. - /// - /// The source. - /// The destination. - public static void MapPrimitivesWithStrings(this object source, object destination) - { - foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive || x.PropertyType == typeof(String))) + /// + /// Maps the object properties values to the destination object including strings and without assigning null string values from the source. + /// + /// The source. + /// The destination. + public static void MapPrimitivesWithStrings(this object source, object destination) { - var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance); - - if (desProp != null && (desProp.PropertyType.IsPrimitive || desProp.PropertyType == typeof(String)) && desProp.SetMethod != null) + foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive || x.PropertyType == typeof(String))) { - var value = prop.GetValue(source); + var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance); + + if (desProp != null && (desProp.PropertyType.IsPrimitive || desProp.PropertyType == typeof(String)) && desProp.SetMethod != null) + { + var value = prop.GetValue(source); - desProp.SetValue(destination, value); + desProp.SetValue(destination, value); + } } } - } - /// - /// Maps the object properties values to the destination object. - /// - /// The source. - /// The destination. - public static void MapPrimitivesTo(this object source, object destination, Func condition) - { - foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive)) + /// + /// Maps the object properties values to the destination object. + /// + /// The source. + /// The destination. + public static void MapPrimitivesTo(this object source, object destination, Func condition) { - var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance); - - if (desProp != null && desProp.PropertyType.IsPrimitive && desProp.SetMethod != null) + foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive)) { - if (condition(prop)) + var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance); + + if (desProp != null && desProp.PropertyType.IsPrimitive && desProp.SetMethod != null) { - desProp.SetValue(destination, prop.GetValue(source)); + if (condition(prop)) + { + desProp.SetValue(destination, prop.GetValue(source)); + } } } } - } - - /// - /// Serializes the specified object to indented json string. - /// - /// The object. - /// - public static String ToJsonString(this Object obj) - { - if (obj == null) return "null"; - if (!_jsonSettingsSet) + /// + /// Serializes the specified object to indented json string. + /// + /// The object. + /// + public static String ToJsonString(this Object obj) { - JsonConvert.DefaultSettings = (() => - { - var settings = new JsonSerializerSettings(); - settings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); - settings.ContractResolver = new ProtobufContractResolver(); - return settings; - }); - - _jsonSettingsSet = true; - } + if (obj == null) return "null"; - return JsonConvert.SerializeObject(obj, Formatting.Indented); - } + if (!_jsonSettingsSet) + { + JsonConvert.DefaultSettings = (() => + { + var settings = new JsonSerializerSettings(); + settings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); + settings.ContractResolver = new ProtobufContractResolver(); + return settings; + }); - /// - /// Serializes the specified object to indented json string. - /// - /// The object. - /// - public static String ToJsonString(this Object obj, params String[] ignoreProperties) - { - var settings = new JsonSerializerSettings() { ContractResolver = new DynamicContractResolver(ignoreProperties) }; - settings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); - return JsonConvert.SerializeObject(obj, Formatting.Indented, settings); - } + _jsonSettingsSet = true; + } - /// - /// Serializes the specified object to indented json html string. - /// - /// The object. - /// - public static String ToHtmlJsonString(this Object obj, params String[] ignoreProperties) - { - var settings = new JsonSerializerSettings() { ContractResolver = new HtmlContractResolver(ignoreProperties) }; - settings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); - return JsonConvert.SerializeObject(obj, Formatting.Indented, settings); - } + return JsonConvert.SerializeObject(obj, Formatting.Indented); + } - /// - /// Gets the property value by the specified path (e.g DateTime.Date.Days). - /// - /// The object. - /// The property path. - /// - public static Object GetPropertyValueByPath(this object obj, String propertyPath) - { - if (propertyPath != null) + /// + /// Serializes the specified object to indented json string. + /// + /// The object. + /// + public static String ToJsonString(this Object obj, params String[] ignoreProperties) { - if (propertyPath.Contains('.')) - { - string[] Split = propertyPath.Split('.'); - string RemainingProperty = propertyPath.Substring(propertyPath.IndexOf('.') + 1); - return GetPropertyValueByPath(obj.GetType().GetProperty(Split[0]).GetValue(obj, null), RemainingProperty); - } - else - return obj.GetType().GetProperty(propertyPath).GetValue(obj, null); + var settings = new JsonSerializerSettings() { ContractResolver = new DynamicContractResolver(ignoreProperties) }; + settings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); + return JsonConvert.SerializeObject(obj, Formatting.Indented, settings); } - else + + /// + /// Serializes the specified object to indented json html string. + /// + /// The object. + /// + public static String ToHtmlJsonString(this Object obj, params String[] ignoreProperties) { - return obj.ToString(); + var settings = new JsonSerializerSettings() { ContractResolver = new HtmlContractResolver(ignoreProperties) }; + settings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); + return JsonConvert.SerializeObject(obj, Formatting.Indented, settings); } - } - /// - /// Sets the property value by the specified path (e.g DateTime.Date.Days). - /// - /// The object. - /// The property path. - /// - public static void SetPropertyValueByPath(this object obj, String propertyPath, object value) - { - if (propertyPath != null) + /// + /// Gets the property value by the specified path (e.g DateTime.Date.Days). + /// + /// The object. + /// The property path. + /// + public static Object GetPropertyValueByPath(this object obj, String propertyPath) { - if (propertyPath.Contains('.')) + if (propertyPath != null) { - string[] Split = propertyPath.Split('.'); - string RemainingProperty = propertyPath.Substring(propertyPath.IndexOf('.') + 1); - SetPropertyValueByPath(obj.GetType().GetProperty(Split[0]).GetValue(obj, null), RemainingProperty, value); + if (propertyPath.Contains('.')) + { + string[] Split = propertyPath.Split('.'); + string RemainingProperty = propertyPath.Substring(propertyPath.IndexOf('.') + 1); + return GetPropertyValueByPath(obj.GetType().GetProperty(Split[0]).GetValue(obj, null), RemainingProperty); + } + else + return obj.GetType().GetProperty(propertyPath).GetValue(obj, null); } else { - obj.GetType().GetProperty(propertyPath).SetValue(obj, value); + return obj.ToString(); + } + } + + /// + /// Sets the property value by the specified path (e.g DateTime.Date.Days). + /// + /// The object. + /// The property path. + /// + public static void SetPropertyValueByPath(this object obj, String propertyPath, object value) + { + if (propertyPath != null) + { + if (propertyPath.Contains('.')) + { + string[] Split = propertyPath.Split('.'); + string RemainingProperty = propertyPath.Substring(propertyPath.IndexOf('.') + 1); + SetPropertyValueByPath(obj.GetType().GetProperty(Split[0]).GetValue(obj, null), RemainingProperty, value); + } + else + { + obj.GetType().GetProperty(propertyPath).SetValue(obj, value); + } } } } -- cgit v1.3.1