我的WEB编程世界

路是靠脚走出来的,能力是靠手和大脑练出来的。

原创 遍历类的属性,并给类的属性赋值|取值收藏

遍历类的属性,并给类的属性赋值|取值

平时自己写的,收藏一下。

 public sealed class Common
    
{
        
public static void Charge<oT, nT>(oT oldClass, nT newClass)
        
{
            Type tOld 
= oldClass.GetType();
            Type tNew 
= newClass.GetType();
            
foreach (PropertyInfo piOld in tOld.GetProperties())
            
{
                
foreach (PropertyInfo piNew in tNew.GetProperties())
                
{
                    
if (piOld.Name.Equals(piNew.Name))
                    
{
                        piOld.SetValue(oldClass, piNew.GetValue(newClass, 
null), null);                       
                        
break;
                    }

                }

            }

        }

        
public static void Charges<oT, nT>(ref List<oT> oldClasses, nT[] newClass) where oT : new()
        
{            
            
for (int i = 0; i < newClass.Length; i++)
            
{
                oT ot 
= new oT();
                Charge
<oT, nT>(ot, newClass[i]);
                oldClasses.Add(ot);
            }

        }

    }

发表于 @ 2008年04月10日 14:42:00|评论(loading...)

新一篇: 程序内部通过Code调用URL文件。 | 旧一篇: 使用调度程序构建反应速度更快的应用程序(续..)

用户操作
[即时聊天] [发私信] [加为好友]
不然
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
不然的公告
尼斯湖怪:深水传说
文章分类
    收藏
      存档
      软件项目交易
      Csdn Blog version 3.1a
      Copyright © 不然