Exemplo n.º 1
0
    public void CopyAtoB(WorldMapStrategyKit.Country source, WPM.Country target, string excludedProperties, BindingFlags memberAccess)
    {
        string[] excluded = null;
        if (!string.IsNullOrEmpty(excludedProperties))
        {
            excluded = excludedProperties.Split(new char[1] {
                ','
            }, StringSplitOptions.RemoveEmptyEntries);
        }

        MemberInfo[] miT = target.GetType().GetMembers(memberAccess);
        foreach (MemberInfo Field in miT)
        {
            string name = Field.Name;

            // Skip over any property exceptions
            if (!string.IsNullOrEmpty(excludedProperties) &&
                excluded.Contains(name))
            {
                continue;
            }

            if (Field.MemberType == MemberTypes.Field)
            {
                FieldInfo SourceField = source.GetType().GetField(name);
                if (SourceField == null)
                {
                    continue;
                }

                object SourceValue = SourceField.GetValue(source);
                ((FieldInfo)Field).SetValue(target, SourceValue);
            }
            else if (Field.MemberType == MemberTypes.Property)
            {
                PropertyInfo piTarget    = Field as PropertyInfo;
                PropertyInfo SourceField = source.GetType().GetProperty(name, memberAccess);
                if (SourceField == null)
                {
                    continue;
                }

                if (piTarget.CanWrite && SourceField.CanRead)
                {
                    object SourceValue = SourceField.GetValue(source, null);
                    piTarget.SetValue(target, SourceValue, null);
                }
            }
        }
    }