c# - Return custom dynamic type -
the dto classes we're returning dapper decorated attributes provide metadata each property (used ui), e.g:
public class person { [metadata("first name")] public string firstname { get; set; } [metadata("last name")] public string lastname { get; set; } }
in cases objects returned table may vary customer customer. have set of "common" fields , set of custom ones e.g. person.foo
.
to facilitate intend use dynamic object (derived system.dynamic.dynamicobject
).
public class dynamicperson : dynamicdto { [metadata("first name")] public string firstname { get; set; } [metadata("last name")] public string lastname { get; set; } }
the idea can reflect on instance properties metadata , use mechanism obtain dynamic property metadata.
when run following test:
var query = "select 'ben' firstname, 'foster' lastname, 25 age"; var people = cn.query<dynamicperson>(query);
only instance properties set (firstname , lastname). can make dapper set dynamic properties don't existing on instance?
Comments
Post a Comment