We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
public static partial class Extensions { public static void CopyProperties(this object from, object to, bool includePropertiesFromBaseType = false, string[] excludedProperties = null) { if (to == null) { return; } Dictionary<string, PropertyInfo> sourceProperties = GetProperties(from.GetType(), includePropertiesFromBaseType); Dictionary<string, PropertyInfo> targetProperties = GetProperties(to.GetType(), includePropertiesFromBaseType); IEnumerable commonPropertiesName = sourceProperties.Values.Intersect(targetProperties.Values, new PropertyInfoComparer()).Select(x => x.Name); foreach (var commonPropertyName in commonPropertiesName) { if (excludedProperties != null && excludedProperties.Contains(commonPropertyName)) continue; var hasExcludeAttribute = FindAttribute(targetProperties[commonPropertyName]); if (!hasExcludeAttribute) { var value = sourceProperties[commonPropertyName].GetValue(from, null); if (targetProperties[commonPropertyName].CanWrite) { targetProperties[commonPropertyName].SetValue(to, value, null); } } } }
public static int GetHashCodeOnProperties(this object inspect) { return inspect.GetType().GetTypeInfo().DeclaredProperties.Select(o => o.GetValue(inspect)).GetListHashCode(); } public static int GetListHashCode(this IEnumerable<object> sequence) { return sequence.Where(x => x != null) .Select(item => item.GetHashCode()) .Aggregate((total, nextCode) => total ^ nextCode); } private static bool FindAttribute(PropertyInfo commonProperty) { var attribute = commonProperty.GetCustomAttribute<ExcludeFromCopyPropertyAttribute>(); return attribute != null; } private static Dictionary<string, PropertyInfo> GetProperties(Type type, bool recursive) { if (recursive) { Dictionary<string, PropertyInfo> properties = new Dictionary<string, PropertyInfo>(); var retrievedProperties = GetProperties(type); foreach(var retrievedProperty in retrievedProperties) { properties.Add(retrievedProperty.Key, retrievedProperty.Value); } var typeInfo = type.GetTypeInfo(); while (typeInfo.BaseType != null) { var baseType = typeInfo.BaseType; retrievedProperties = GetProperties(baseType); foreach (var retrievedProperty in retrievedProperties) { if (!properties.ContainsKey(retrievedProperty.Key)) { properties.Add(retrievedProperty.Key, retrievedProperty.Value); } } typeInfo = baseType.GetTypeInfo(); } return properties; } return GetProperties(type); } private static Dictionary<string, PropertyInfo> GetProperties(Type type) { var typeInfo = type.GetTypeInfo(); Dictionary<string, PropertyInfo> properties = new Dictionary<string, PropertyInfo>(); foreach (var property in typeInfo.DeclaredProperties) { properties.Add(property.Name, property); } return properties; } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
public static partial class Extensions
{
public static void CopyProperties(this object from, object to, bool includePropertiesFromBaseType = false, string[] excludedProperties = null)
{
if (to == null)
{
return;
}
Dictionary<string, PropertyInfo> sourceProperties = GetProperties(from.GetType(), includePropertiesFromBaseType);
Dictionary<string, PropertyInfo> targetProperties = GetProperties(to.GetType(), includePropertiesFromBaseType);
IEnumerable commonPropertiesName = sourceProperties.Values.Intersect(targetProperties.Values, new PropertyInfoComparer()).Select(x => x.Name);
foreach (var commonPropertyName in commonPropertiesName)
{
if (excludedProperties != null
&& excludedProperties.Contains(commonPropertyName))
continue;
var hasExcludeAttribute = FindAttribute(targetProperties[commonPropertyName]);
if (!hasExcludeAttribute)
{
var value = sourceProperties[commonPropertyName].GetValue(from, null);
if (targetProperties[commonPropertyName].CanWrite)
{
targetProperties[commonPropertyName].SetValue(to, value, null);
}
}
}
}
The text was updated successfully, but these errors were encountered: