Skip to content
New issue

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

code aanpassing voor extensions #20

Open
deRightDirection opened this issue Jun 6, 2018 · 0 comments
Open

code aanpassing voor extensions #20

deRightDirection opened this issue Jun 6, 2018 · 0 comments

Comments

@deRightDirection
Copy link
Collaborator

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;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

0 participants