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);
}
精彩评论