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

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 优雅旋律

四角号码 对照表 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已测试通过