diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-07-07 15:05:28 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-07-07 15:05:28 +0300 |
| commit | 081d332a0494d309c2762f76f9dc5032a673ea0d (patch) | |
| tree | f8fcbfb986447168945454f394380ff240f7a471 /Software/Visual_Studio/Tango.Core | |
| parent | 30926a8078c75c5d8c4ed39b053f072940c30d9a (diff) | |
| download | Tango-081d332a0494d309c2762f76f9dc5032a673ea0d.tar.gz Tango-081d332a0494d309c2762f76f9dc5032a673ea0d.zip | |
Removed Object extension methods from global namespace.
Diffstat (limited to 'Software/Visual_Studio/Tango.Core')
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs | 331 |
1 files changed, 167 insertions, 164 deletions
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; -/// <summary> -/// Contains <see cref="Object"/> extension methods. -/// </summary> -public static class ObjectExtensions +namespace Tango.Core.ExtensionMethods { - private static bool _jsonSettingsSet; - /// <summary> - /// Performs shallow cloning of the object excluding generic properties.. + /// Contains <see cref="Object"/> extension methods. /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="obj">The object.</param> - /// <returns></returns> - public static T ShallowClone<T>(this T obj) + public static class ObjectExtensions { - var cloned = Activator.CreateInstance<T>(); + private static bool _jsonSettingsSet; - foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.SetMethod != null)) + /// <summary> + /// Performs shallow cloning of the object excluding generic properties.. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="obj">The object.</param> + /// <returns></returns> + public static T ShallowClone<T>(this T obj) { - if (!prop.PropertyType.IsGenericType) + var cloned = Activator.CreateInstance<T>(); + + 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; + } - /// <summary> - /// Performs a shallow mapping to the specified object excluding generic properties. - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="source">The source.</param> - /// <param name="destination">The destination object.</param> - public static void ShallowCopyTo<T>(this T source, T destination) - { - foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.SetMethod != null)) + /// <summary> + /// Performs a shallow mapping to the specified object excluding generic properties. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="source">The source.</param> + /// <param name="destination">The destination object.</param> + public static void ShallowCopyTo<T>(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)); + } } } - } - /// <summary> - /// Maps the object properties values to the destination object. - /// </summary> - /// <param name="source">The source.</param> - /// <param name="destination">The destination.</param> - 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)) + /// <summary> + /// Maps the object properties values to the destination object. + /// </summary> + /// <param name="source">The source.</param> + /// <param name="destination">The destination.</param> + 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)); + } } } - } - /// <summary> - /// Maps the object properties values to the destination object including strings and without assigning null string values from the source. - /// </summary> - /// <param name="source">The source.</param> - /// <param name="destination">The destination.</param> - 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))) + /// <summary> + /// Maps the object properties values to the destination object including strings and without assigning null string values from the source. + /// </summary> + /// <param name="source">The source.</param> + /// <param name="destination">The destination.</param> + 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); + } } } } - } - /// <summary> - /// Maps the object properties values to the destination object including strings and without assigning null string values from the source. - /// </summary> - /// <param name="source">The source.</param> - /// <param name="destination">The destination.</param> - 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))) + /// <summary> + /// Maps the object properties values to the destination object including strings and without assigning null string values from the source. + /// </summary> + /// <param name="source">The source.</param> + /// <param name="destination">The destination.</param> + 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); - desProp.SetValue(destination, value); + if (desProp != null && (desProp.PropertyType.IsPrimitive || desProp.PropertyType == typeof(String)) && desProp.SetMethod != null) + { + var value = prop.GetValue(source); + + desProp.SetValue(destination, value); + } } } - } - /// <summary> - /// Maps the object properties values to the destination object. - /// </summary> - /// <param name="source">The source.</param> - /// <param name="destination">The destination.</param> - public static void MapPrimitivesTo(this object source, object destination, Func<PropertyInfo, bool> condition) - { - foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive)) + /// <summary> + /// Maps the object properties values to the destination object. + /// </summary> + /// <param name="source">The source.</param> + /// <param name="destination">The destination.</param> + public static void MapPrimitivesTo(this object source, object destination, Func<PropertyInfo, bool> 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)); + } } } } - } - - /// <summary> - /// Serializes the specified object to indented json string. - /// </summary> - /// <param name="obj">The object.</param> - /// <returns></returns> - public static String ToJsonString(this Object obj) - { - if (obj == null) return "null"; - if (!_jsonSettingsSet) + /// <summary> + /// Serializes the specified object to indented json string. + /// </summary> + /// <param name="obj">The object.</param> + /// <returns></returns> + 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; - }); + if (obj == null) return "null"; - _jsonSettingsSet = true; - } - - 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; + }); - /// <summary> - /// Serializes the specified object to indented json string. - /// </summary> - /// <param name="obj">The object.</param> - /// <returns></returns> - 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; + } - /// <summary> - /// Serializes the specified object to indented json html string. - /// </summary> - /// <param name="obj">The object.</param> - /// <returns></returns> - 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); + } - /// <summary> - /// Gets the property value by the specified path (e.g DateTime.Date.Days). - /// </summary> - /// <param name="obj">The object.</param> - /// <param name="propertyPath">The property path.</param> - /// <returns></returns> - public static Object GetPropertyValueByPath(this object obj, String propertyPath) - { - if (propertyPath != null) + /// <summary> + /// Serializes the specified object to indented json string. + /// </summary> + /// <param name="obj">The object.</param> + /// <returns></returns> + 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 + + /// <summary> + /// Serializes the specified object to indented json html string. + /// </summary> + /// <param name="obj">The object.</param> + /// <returns></returns> + 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); } - } - /// <summary> - /// Sets the property value by the specified path (e.g DateTime.Date.Days). - /// </summary> - /// <param name="obj">The object.</param> - /// <param name="propertyPath">The property path.</param> - /// <returns></returns> - public static void SetPropertyValueByPath(this object obj, String propertyPath, object value) - { - if (propertyPath != null) + /// <summary> + /// Gets the property value by the specified path (e.g DateTime.Date.Days). + /// </summary> + /// <param name="obj">The object.</param> + /// <param name="propertyPath">The property path.</param> + /// <returns></returns> + 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(); + } + } + + /// <summary> + /// Sets the property value by the specified path (e.g DateTime.Date.Days). + /// </summary> + /// <param name="obj">The object.</param> + /// <param name="propertyPath">The property path.</param> + /// <returns></returns> + 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); + } } } } |
