Skip to content

code aanpassing voor extensions #20

Description

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;
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions