2011-08-25 67 views
1

我有一個方法UniversalConverter的問題。從數據庫接收的行應該被轉移到實體中。所以行[pi.Name]必須轉換爲實體類型。如何執行此操作?PropertyInfo SetValue類型轉換

public void GetRequestSummary(string UserName, string Password) 
    { 
     List<EducationRequest> requestSummary = new List<EducationRequest>(); 
     OracleCommand command = new OracleCommand(); 

     AddParameter(command, _usernamePN, DbType.String, UserName); 
     AddParameter(command, _passwordPN, DbType.String, Password); 
     AddOracleCursor(command, _curPN, OracleType.Cursor, ParameterDirection.Output); 

     command.CommandType = CommandType.StoredProcedure; 

     DataSet personaSet = 
      FillDataset(command, _GetRequestSummaryCmd); 

     DataTable personaTable = 
      personaSet.Tables[0]; 

     var request = new EducationRequestSummary(); 
     foreach (DataRow row in personaTable.Rows) 
     { 
      UniversalConverter(request, row); 
     } 

     //any implementation 

    } 

public void UniversalConverter<T>(T entity, DataRow row) 
    { 

     foreach (var pi in typeof(T).GetProperties()) 
     { 
      pi.SetValue(entity, row[pi.Name], null); 
     } 
    } 

回答

1

你需要或者類似的東西Automapper

,或者您需要映射這個手動的對象,甚至當你在你的代碼提供了使用反射。在那種情況下,Kepp注意到屬性名稱和類型。

希望這會有所幫助。

+0

謝謝! Automapper非常有用) –

+1

Automapper很好。其他正在觀看的人...嘗試ValueInjector。這篇文章幫助我做了一些與以下內容類似的東西:http://stackoverflow.com/questions/1398796/casting-with-reflection – FastAl