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
.
This means converting strings to property names on the fly, so I thought about creating a string like this:
"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
.
Also note that sadly, I don't have the access to this framework functionality and cannot edit it.
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
.
QUESTIONS
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?