惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Last Week in AI
Last Week in AI
D
DataBreaches.Net
腾讯CDC
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
罗磊的独立博客
月光博客
月光博客
MyScale Blog
MyScale Blog
U
Unit 42
Martin Fowler
Martin Fowler
Stack Overflow Blog
Stack Overflow Blog
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
G
Google Developers Blog
博客园 - 【当耐特】
D
Docker
I
InfoQ
雷峰网
雷峰网

博客园 - 优雅旋律

四角号码 对照表 ScriptManager Bug Application_Start 不执行 loading 关机效果 ZT Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景] ZT 数据库范式浅解 ZT 数据库设计系列:数据库设计5步骤 ZT Web Control 开发系列(一) 页面的生命周期 MarcHandler (Marc ISO2709) Resharper 4.0 终于RC了... GridView、DetailsView、FormView 、Repeater、DataList的区别 (ZT) 常用正则表达式 ZT 利用对象序列化将购物车保存在Cookie中 (ZT) ajax tab 动态设置主题(Theme) 一些注意 持久化数据 关于vs2008的Eval方法 重大失误 datapager分页问题 (点击两次)
linq(update) 更新用 自动赋值方法
优雅旋律 · 2008-04-28 · via 博客园 - 优雅旋律

/// <summary>
        
/// 同类型数据赋值
        
/// </summary>
        
/// <param name="DataSetter">需要设置值的对象</param>
        
/// <param name="DataGetter">值来源</param> 
        
/// <param name="IsLinqData">是否为linq模式</param>

        public static void SameValueCopier(object DataSetter, object DataGetter,bool IsLinqData)
        
{

            PropertyInfo[] propertyInfoList 
= DataSetter.GetType().GetProperties();
            
foreach (PropertyInfo propertyInfo in propertyInfoList)
            
{
                
if (propertyInfo.MemberType == MemberTypes.Property)
                
{
                    
string propertyName = propertyInfo.Name;
                    
object value = DataGetter.GetType().GetProperty(propertyName).GetValue(DataGetter, null);
                    
if (IsLinqData)
                    
{
                        
if (!propertyInfo.PropertyType.IsGenericType)
                        
{  
                            DataSetter.GetType().GetProperty(propertyName).SetValue(DataSetter, value, 
null);
                        }

                    }

                    
else
                    
{  
                        DataSetter.GetType().GetProperty(propertyName).SetValue(DataSetter, value, 
null);
                    }

                    
                }

            }

        }



        
/// <summary>
        
/// 同类型数据赋值
        
/// </summary>
        
/// <param name="DataSetter">需要设置值的对象</param>
        
/// <param name="DataGetter">值来源</param> 
        
/// <param name="IsLinqData">是否为linq模式</param>

        public static void DiffValueCopier(object DataSetter, object DataGetter,bool IsLinqData)
        
{

            PropertyInfo[] propertyInfoSetterList 
= DataSetter.GetType().GetProperties();
            PropertyInfo[] propertyInfoGetterList 
= DataGetter.GetType().GetProperties();

            
foreach (PropertyInfo dataSetterPropertyInfo in propertyInfoSetterList)
            
{
                
if (dataSetterPropertyInfo.MemberType == MemberTypes.Property)
                
{
                    
string dataSetterPropertyName =  dataSetterPropertyInfo.Name;

                    
foreach (PropertyInfo dataGetterPropertyInfo in propertyInfoGetterList)
                    
{
                        
string dataGetterPropertyName = dataGetterPropertyInfo.Name;
                        
object value = DataGetter.GetType().GetProperty(dataGetterPropertyName).GetValue(DataGetter, null);
                        
if (dataSetterPropertyName == dataGetterPropertyName)
                        
{
                            
if (IsLinqData)
                            
{
                                
if (!dataSetterPropertyInfo.PropertyType.IsGenericType)
                                
{
                                    DataSetter.GetType().GetProperty(dataSetterPropertyName).SetValue(DataSetter, value, 
null);
                                    
break;
                                }

                            }

                            
else
                            
{
                                DataSetter.GetType().GetProperty(dataSetterPropertyName).SetValue(DataSetter, value, 
null);
                                
break;
                            }

                        }

                    }
 
                }

            }

        }

            Category newc = new Category { ID = "1", P_ID = "0", Name = "b" };
            Category oldc = db.Category.Where(r => r.ID == "1").First();

            SameValueCopier(oldc, newc);
            db.SubmitChanges();
            kmdc.SubmitChanges();

SameValueCopier已测试通过