实体属性与类型对象转换(未完成)

记录一段代码,未完成版。
想法:是根据传为和实体的一些属性转为相应的类型对像,
把相应的类型对象转换为对应的实体属性

//实体转为对象
 public void EntityToMyObject(object objEntity,object myObj)
  {
   Type tEntity = objEntity.GetType();
   Type tMyObj=myObj.GetType();

   // properties.
   PropertyInfo [] piEntity;
   PropertyInfo [] piMyObj;

   piEntity = tEntity.GetProperties (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
   piMyObj=tMyObj.GetProperties (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
    foreach (PropertyInfo piE in piEntity)
    {
     object obj=piE.GetValue(objEntity,null);
     foreach(PropertyInfo piO in piMyObj )
     {
     if(piE.Name.ToLower()==piO.Name.ToLower())
     {
      piO.SetValue(myObj,piE.GetValue(objEntity,null),null);
      break;
     }
     }
     
    }
    
  

  }
//对象转为实体

  public void MyObjectToEntity(object objEntity,object myObj)
  {
   Type tEntity = objEntity.GetType();
   Type tMyObj=myObj.GetType();

   // properties.
   PropertyInfo [] piEntity;
   PropertyInfo [] piMyObj;

   piEntity = tEntity.GetProperties (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
   piMyObj=tMyObj.GetProperties (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
   foreach(PropertyInfo piO in piMyObj)
   
   {
    object obj=piO.GetValue(myObj,null);
    foreach (PropertyInfo piE in piEntity)
    {
     if( piO.PropertyType.Name=="DateTime")
     {
      if(piE.Name.ToLower()==piO.Name.ToLower() )
      {
       
       if(new FreightAgent.Rule.Validator().CheckDateTimeIsTrue(obj))
        piE.SetValue(objEntity,obj,null);
       break;
      }
     }
     else
      if(piE.Name.ToLower()==piO.Name.ToLower() && obj.ToString()!=String.Empty)
     {
      piE.SetValue(objEntity,obj,null);
      break;
     }
     
    }
     
   }
    
  

  }

posted @ 2006-03-28 16:14  DotNet菜园  阅读(565)  评论(0编辑  收藏  举报