在不知道列名和计数的情况下将DataTable.Rows转换为List <my_class>

As input for this problem I have a DataTable instance. For some framework functions I have to use, I need to pass rows from this DataTable as List<my_class> where properties of my_class are named like columns of the input DataTable.
Maybe a better explanation is: instead of getting a cell value like DataTableName.Rows[0]["first_column], I'd have to get it as myList[0].first_row.

这意味着可以立即将字符串转换为属性名称,所以我考虑过创建这样的字符串:

"namespace NewRow 
{
    class my_class
    {
        public string first_column { get; set; }
        public string second_column { get; set; }
        ...
    }
}"

then using CSharpCodeProvider to compile this string and then create a List<my_class>.

After I use var codeResult = provider.CompileAssemblyFromSource(new CompilerParameters(), codeAsString); I don't know what to do next. How do I get the new class as a type?

NOTES
I unsuccessfully tried passing List<object> to my framework functions. It doesn't matter if this list is populated with anonymous object with properly named properties or a hardcoded my_class with properly named properties, I get the same error saying that my object (from the list) doesn't contain a property named first_column.

另请注意,遗憾的是,我无权使用此框架功能,因此无法对其进行编辑。

Framework's function actually expects a IQueryable, but I thought it would be easier to work with a List and then convert it to IQueryable just before passing this list to the function (I can do this by using my_list.AsQueryable() function), but if it helps solve the problem, it's ok to use the IQueryable.

问题

Am I on the right track with CSharpCodeProvider?
If so, how do I create a List<my_class> on the fly?
Is there another way I'm not aware of for doing this kind of conversion?

评论