What's the easiest way to generate code dynamically in .NET 4.5?

前端 未结 4 1107
旧时难觅i
旧时难觅i 2020-12-31 23:41

I\'m writing a specific kind of object-mapper. Basically I want to convert from a DataTable that has the fields a, b and c

4条回答
  •  借酒劲吻你
    2021-01-01 00:31

    Late to the party, but Marc Gravell has a nice utility called FastMember. With FastMember, you can map from a DataTable to an object, even a dynamic.

    var accessor = TypeAccessor.Create(type);
    string propName = // something known only at runtime
    
    while( /* some loop of data */ ) {
       accessor[obj, propName] = rowValue;
    }
    

    I'ved used this in production and it performs nicely.

提交回复
热议问题