玄铁剑

成功的途径:抄,创造,研究,发明...
posts - 128, comments - 42, trackbacks - 0, articles - 174

对象拷贝赋值

Posted on 2010-07-14 21:13 玄铁剑 阅读(540) 评论(0)  编辑 收藏 引用 所属分类: asp.netC# Winform

        public static void CopyObjectData(object source, object target)
        {  
            MemberInfo[] miT = target.GetType().GetMembers();  
            foreach (MemberInfo Field in miT)  
            {  
                string name = Field.Name;
                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);
                }  
                if (Field.MemberType == MemberTypes.Property)  
                {  
                    PropertyInfo piTarget = Field as PropertyInfo;  
                    PropertyInfo SourceField = source.GetType().GetProperty(name);
                    if (SourceField == null)  
                        continue;
                    string[] obj = { "EntityKey", "EntityState" };
                    if (SourceField.Name.In(obj, false) == true)
                        continue;
                    if (piTarget.CanWrite && SourceField.CanRead)  
                    {  
                        object SourceValue = SourceField.GetValue(source, null);  
                        piTarget.SetValue(target, SourceValue, null);  
                    }  
                }  
            }  
        }



/// <summary>  
/// Copies the data of one object to another. The target object 'pulls' properties of the first.   
/// This any matching properties are written to the target.  
///   
/// The object copy is a shallow copy only. Any nested types will be copied as   
/// whole values rather than individual property assignments (ie. via assignment)  
/// </summary>  
/// <param name="source">The source object to copy from</param>  
/// <param name="target">The object to copy to</param>  
/// <param name="excludedProperties">A comma delimited list of properties that should not be copied</param>  
/// <param name="memberAccess">Reflection binding access</param>  
public static void CopyObjectData(object source, object 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);  
            }  
        }  
    }  

只有注册用户登录后才能发表评论。