PropertyInfo SetValue 类型转换
我对 UniversalConverter 方法有疑问。从数据库收到的行应传输到实体。因此 row[pi.Name] 必须转换为实体类型。如何执行此操作?
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);
}
}
I have a problem with method UniversalConverter. Row received from database should be transferred in to entity. So row[pi.Name] must be converted in to entity type. How to perform this?
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要类似 Automapper 的东西
,或者您需要手动映射此对象,即使使用您在代码。在这种情况下,Kepp 显然要注意属性名称和类型。
希望这有帮助。
You need or something like Automapper
or you need to map this objects manually, even by using reflection as you provided in your code. Kepp attention property names and types obviously in that cases.
Hope this helps.