开发者

Using AutoMapper to map object fields to array?

开发者 https://www.devze.com 2023-02-10 17:29 出处:网络
Is it possible with automapper in C# to map the properties of an object to an array/dictionary? I have tried the following:

Is it possible with automapper in C# to map the properties of an object to an array/dictionary? I have tried the following:

Mapper.CreateMap<FFCLeads.Models.FFCLead, Dictionary<string, SqlParameter>>()
    .ForMember(d => d["LeadID"], o => o.MapFrom(s => new SqlParameter("LeadID", s.LeadID)))
    .ForMember(d => d["LastName"], o => o.MapFrom(s => new SqlParameter("LastName", s.LastName)));

However, it does not work (object ref not set to an instance). Basically, I'm trying to make the values of this object to an array of SqlParameter objects. Possible? If so, what is the correct way to do this?开发者_Go百科 Thanks.


I use the following method:

IDictionary<string, object> GetDictionaryFromObject(object obj)
{
    if(obj == null) return new Dictionary<string, object>();
    return obj.GetType().GetProperties().
                ToDictionary(p => p.Name,
                             p => p.GetValue(obj, null) ?? DBNull.Value);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消