首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PropertyInfo SetValue类型转换

PropertyInfo SetValue类型转换
EN

Stack Overflow用户
提问于 2011-08-25 18:57:44
回答 1查看 1.7K关注 0票数 1

我对UniversalConverter方法有一个问题。从数据库接收的行应传输到实体。因此必须将rowpi.Name转换为实体类型。如何执行此操作?

代码语言:javascript
复制
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);
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-25 19:01:31

你需要或者像Automapper这样的东西

或者,您需要手动映射此对象,即使使用代码中提供的反射也是如此。在这种情况下,Kepp会注意属性名称和类型。

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7189183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档