diff --git a/Extensions/ModelExtensions.cs b/Extensions/ModelExtensions.cs index 97e8090..56b34f8 100644 --- a/Extensions/ModelExtensions.cs +++ b/Extensions/ModelExtensions.cs @@ -4,8 +4,10 @@ using System.Collections.ObjectModel; using System.Linq; using System.Linq.Expressions; -namespace xCommons.Extensions { - public static partial class ModelExtensions { +namespace xCommons.Extensions +{ + public static partial class ModelExtensions + { /// /// Update an Instance of a Typed Object with Data Provided by another Instance of Typed Object /// @@ -19,7 +21,7 @@ namespace xCommons.Extensions { /// /// /// - public static T UpdateData ( + public static T UpdateData( this T source, D updateWith, ICollection propertyWhiteList = null, @@ -29,58 +31,65 @@ namespace xCommons.Extensions { bool throwExceptionOnFails = false ) where T : class - where D : class { + where D : class + { // - var hasWhiteList = propertyWhiteList.HasChild (); - var hasBlackList = propertyBlackList.HasChild (); - var hasValueProvider = propertyValueProviders.HasChild>> (); + var hasWhiteList = propertyWhiteList.HasChild(); + var hasBlackList = propertyBlackList.HasChild(); + var hasValueProvider = propertyValueProviders.HasChild>>(); // - var fullPropertyList = updateWith.GetType ().GetProperties ().Select (prop => prop.Name); + var fullPropertyList = updateWith.GetType().GetProperties().Select(prop => prop.Name); var mustSetProps = (hasWhiteList ? propertyWhiteList : fullPropertyList); // - if (hasBlackList) { - mustSetProps = mustSetProps.Where (propName => !propertyBlackList.Contains (propName)); + if (hasBlackList) + { + mustSetProps = mustSetProps.Where(propName => !propertyBlackList.Contains(propName)); } // - mustSetProps.ToList ().ForEach (propName => { + mustSetProps.ToList().ForEach(propName => + { // - try { + try + { // - var sourceProp = source.GetType ().GetProperty (propName); - var sourceValue = sourceProp.GetValue (source); - var sourceValueType = sourceValue.IsNull () ? null : sourceValue.GetType (); + var sourceProp = source.GetType().GetProperty(propName); + var sourceValue = sourceProp.GetValue(source); + var sourceValueType = sourceValue.IsNull() ? null : sourceValue.GetType(); // - var updateWithProp = updateWith.GetType ().GetProperty (propName); - var updateWithValue = updateWithProp.GetValue (updateWith); - var updateWithValueType = updateWithValue.IsNull () ? null : updateWithValue.GetType (); + var updateWithProp = updateWith.GetType().GetProperty(propName); + var updateWithValue = updateWithProp.GetValue(updateWith); + var updateWithValueType = updateWithValue.IsNull() ? null : updateWithValue.GetType(); // var valueProviderValue = hasValueProvider ? propertyValueProviders - .FirstOrDefault (p => p.Key == propName).Value : null; - var updateVal = !valueProviderValue.IsNull () ? - valueProviderValue.Invoke (updateWith) : - (updateWithValue.IsNull () && !updateWithNullOrEmptyValues) ? + .FirstOrDefault(p => p.Key == propName).Value : null; + var updateVal = !valueProviderValue.IsNull() ? + valueProviderValue.Invoke(updateWith) : + (updateWithValue.IsNull() && !updateWithNullOrEmptyValues) ? sourceValue : updateWithValue; - var updateValType = updateVal.IsNull () ? null : updateVal.GetType (); + var updateValType = updateVal.IsNull() ? null : updateVal.GetType(); // - Type t = Nullable.GetUnderlyingType (sourceProp.PropertyType) ?? sourceProp.PropertyType; + Type t = Nullable.GetUnderlyingType(sourceProp.PropertyType) ?? sourceProp.PropertyType; object safeValue = (updateVal == null) ? null : - !updateValType.IsCollectionType () ? - Convert.ChangeType (updateVal, t) : + !updateValType.IsCollectionType() ? + Convert.ChangeType(updateVal, t) : updateVal; // - sourceProp.SetValue (source, safeValue); - } catch (Exception ex) { + sourceProp.SetValue(source, safeValue); + } + catch (Exception ex) + { // - if (throwExceptionOnFails) { + if (throwExceptionOnFails) + { throw ex; } } @@ -90,6 +99,114 @@ namespace xCommons.Extensions { return source; } + /// + /// Check a Class is Contains a Property or not ... + /// + /// + /// + /// + /// + public static bool HasProperty( + this T source, + string property + ) + where T : class + { + // + var result = source + .GetType() + .GetProperties() + .Any(prop => prop.Name == property); ; + + // + return result; + } + + /// + /// Get a Class Property Value using Generic Concept ... + /// + /// + /// + /// + /// + /// + public static TProp GetProperty( + this T source, + string property + ) + where T : class + { + // + TProp result = default(TProp); + + // + // Check if T Has Property ... + if (source.HasProperty(property)) + { + // + // Access Property ... + var prop = source + .GetType() + .GetProperty(property); + + // + // Retrieve Property Value as Object ... + var propVal = prop.GetValue(source); + + // + result = (TProp)propVal; + } + + // + return result; + } + + /// + /// Setting Property Value of T ... + /// + /// + /// + /// + /// + /// + /// + public static bool SetProperty( + this T source, + string property, + TProp value + ) + where T : class + { + // + // Check T Has Property ... + var result = source.HasProperty(property); + if (!result) + { + return result; + } + + // + // Access Property ... + var prop = source + .GetType() + .GetProperty(property); + + // + // Set Property ... + try + { + // + result = true; + prop.SetValue(source, value); + } + catch + { + result = false; + } + + // + return result; + } /// /// Check two Instance of Typed Object Values are Same or not @@ -103,7 +220,7 @@ namespace xCommons.Extensions { /// /// /// /// - public static bool IsSameContent ( + public static bool IsSameContent( this T source, D dest, ICollection propertyWhiteList = null, @@ -112,27 +229,31 @@ namespace xCommons.Extensions { bool throwExceptionOnFails = false ) where T : class - where D : class { + where D : class + { // - var hasWhiteList = propertyWhiteList.HasChild (); - var hasBlackList = propertyBlackList.HasChild (); - var hasCheckerProvider = propertyValueCheckers.HasChild>> (); + var hasWhiteList = propertyWhiteList.HasChild(); + var hasBlackList = propertyBlackList.HasChild(); + var hasCheckerProvider = propertyValueCheckers.HasChild>>(); // - var fullPropertyList = dest.GetType ().GetProperties ().Select (prop => prop.Name); + var fullPropertyList = dest.GetType().GetProperties().Select(prop => prop.Name); var mustCheckProps = (hasWhiteList ? propertyWhiteList : fullPropertyList); - if (hasBlackList) { - mustCheckProps = mustCheckProps.Where (propName => !propertyBlackList.Contains (propName)); + if (hasBlackList) + { + mustCheckProps = mustCheckProps.Where(propName => !propertyBlackList.Contains(propName)); } // var result = true; - mustCheckProps.ToList ().ForEach (propName => { + mustCheckProps.ToList().ForEach(propName => + { // - try { + try + { // - var sourceProp = source.GetType ().GetProperty (propName); - var destProp = dest.GetType ().GetProperty (propName); + var sourceProp = source.GetType().GetProperty(propName); + var destProp = dest.GetType().GetProperty(propName); // // Check Types ... @@ -141,29 +262,36 @@ namespace xCommons.Extensions { // // Check var isSameValue = false; - var mustDoCustomCheck = hasCheckerProvider && propertyValueCheckers.Any (p => p.Key == propName); - if (mustDoCustomCheck) { + var mustDoCustomCheck = hasCheckerProvider && propertyValueCheckers.Any(p => p.Key == propName); + if (mustDoCustomCheck) + { // // Check Values ... - var valueChecker = propertyValueCheckers.FirstOrDefault (p => p.Key == propName).Value; - isSameValue = valueChecker.Invoke (source, dest); - } else { + var valueChecker = propertyValueCheckers.FirstOrDefault(p => p.Key == propName).Value; + isSameValue = valueChecker.Invoke(source, dest); + } + else + { // // // isSameType = (sourceProp.PropertyType == destProp.PropertyType && // sourceProp.ReflectedType == destProp.ReflectedType); // // Check Values ... - isSameValue = Equals (sourceProp.GetValue (source), destProp.GetValue (dest)); + isSameValue = Equals(sourceProp.GetValue(source), destProp.GetValue(dest)); } // - if (!isSameType || !isSameValue) { + if (!isSameType || !isSameValue) + { result = false; } - } catch (Exception ex) { + } + catch (Exception ex) + { // - if (throwExceptionOnFails) { + if (throwExceptionOnFails) + { throw ex; } } @@ -178,19 +306,21 @@ namespace xCommons.Extensions { /// /// /// - public static bool IsCollectionType (this Type source) { + public static bool IsCollectionType(this Type source) + { // - if (source.IsNull () || !source.IsGenericType) { + if (source.IsNull() || !source.IsGenericType) + { return false; } // - var genericTypeDefinition = source.GetGenericTypeDefinition (); - var result = genericTypeDefinition == typeof (List<>) || - genericTypeDefinition == typeof (HashSet<>) || - genericTypeDefinition == typeof (Collection<>) || - genericTypeDefinition == typeof (ICollection<>) || - genericTypeDefinition == typeof (IEnumerable<>); + var genericTypeDefinition = source.GetGenericTypeDefinition(); + var result = genericTypeDefinition == typeof(List<>) || + genericTypeDefinition == typeof(HashSet<>) || + genericTypeDefinition == typeof(Collection<>) || + genericTypeDefinition == typeof(ICollection<>) || + genericTypeDefinition == typeof(IEnumerable<>); // return result; @@ -202,14 +332,15 @@ namespace xCommons.Extensions { /// /// /// - public static string GetPropValues (this T item) - where T : class { + public static string GetPropValues(this T item) + where T : class + { // - var props = item.GetType ().GetProperties (); - var vals = props.Select (p => p.GetValue (item)); + var props = item.GetType().GetProperties(); + var vals = props.Select(p => p.GetValue(item)); // - return vals.ToJSON (); + return vals.ToJSON(); } /// @@ -219,13 +350,14 @@ namespace xCommons.Extensions { /// /// /// - public static bool PropValuesContains (this T item, string value) - where T : class { + public static bool PropValuesContains(this T item, string value) + where T : class + { // - return item.GetPropValues () - .ToNormalString () - .Contains (value - .ToNormalString ()); + return item.GetPropValues() + .ToNormalString() + .Contains(value + .ToNormalString()); } /// @@ -234,20 +366,23 @@ namespace xCommons.Extensions { /// /// /// - public static IDictionary>> GetDefaultColumnsMap (this T item) - where T : class { + public static IDictionary>> GetDefaultColumnsMap(this T item) + where T : class + { // - if (item.IsNull ()) { + if (item.IsNull()) + { return null; } // - var result = new Dictionary>> (); - var props = item.GetType ().GetProperties (); + var result = new Dictionary>>(); + var props = item.GetType().GetProperties(); // - foreach (var prop in props) { - result.Add (prop.Name, t => t.GetType ().GetProperty (prop.Name).GetValue (t, null)); + foreach (var prop in props) + { + result.Add(prop.Name, t => t.GetType().GetProperty(prop.Name).GetValue(t, null)); } // @@ -260,20 +395,23 @@ namespace xCommons.Extensions { /// /// /// - public static IDictionary GetPropValuesDictionary (this T item) - where T : class { + public static IDictionary GetPropValuesDictionary(this T item) + where T : class + { // - if (item.IsNull ()) { + if (item.IsNull()) + { return null; } // - var result = new Dictionary (); - var props = item.GetType ().GetProperties (); + var result = new Dictionary(); + var props = item.GetType().GetProperties(); // - foreach (var prop in props) { - result.Add (prop.Name, prop.GetValue (item).ToJSON ()); + foreach (var prop in props) + { + result.Add(prop.Name, prop.GetValue(item).ToJSON()); } //