Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic class creation in C#

Tags:

c#

dynamic

class

Is it possible in runtime to create a class from DataTable where ColumnName will be dynamic-class properties?

like image 829
Polaris Avatar asked Apr 23 '10 12:04

Polaris


1 Answers

With C# 4, you can do this

dynamic foo = new ExpandoObject();

// mimic grabbing a column name at runtime and adding it as a property
((IDictionary<string, object>)foo).Add("Name", "Apple");

Console.WriteLine(foo.Name); // writes Apple to screen

Not recommending it or anything, but it shows you it is possible.

like image 164
Anthony Pegram Avatar answered Sep 27 '22 18:09

Anthony Pegram