Is it possible in runtime to create a class from DataTable where ColumnName will be dynamic-class properties?
-
Why? What are you trying to do? – SLaks Apr 23 '10 at 12:56
-
You mean define a new class, or dynamically set an existing class properties? – JustLoren Apr 23 '10 at 12:56
-
I want to create new class with dataTable column names – Polaris Apr 23 '10 at 12:59
-
Why can't you create the classes in design time? – Cameron MacFarland Apr 23 '10 at 17:03
-
I just added the code in my post. Sorry for the comment if you already saw – Erup May 09 '10 at 18:11
-
possible duplicate of [How do I create dynamic properties in C#?](http://stackoverflow.com/questions/947241/how-do-i-create-dynamic-properties-in-c) – nawfal Jul 20 '14 at 06:32
5 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.

- 123,721
- 27
- 225
- 246
Yes (using Reflection.Emit), but it's a bad idea.
What are you trying to do?

- 868,454
- 176
- 1,908
- 1,964
-
I have RadGridView which has some problems when I use dataTable as source for him. But when I use List
everything works fine. Any ideas? – Polaris Apr 23 '10 at 12:58 -
1
Reading your comments, I undestood your mean. Just use Generics: using List fields to generate the objects. The code is quite simple:
public class DynClass<T, P>
{
public DynClass()
{
_fields = new Dictionary<T, P>();
}
private IDictionary<T, P> _fields;
public IDictionary<T, P> Fields
{
get { return _fields; }
}
}
public class TestGenericInstances
{
public TestGenericInstances()
{
Client cli = new Client("Ash", "99999999901");
/* Here you can create any instances of the Class.
* Also DynClass<string, object>
* */
DynClass<string, Client> gen = new DynClass<string, Client>();
/* Add the fields
* */
gen.Fields.Add("clientName", cli);
/* Add the objects to the List
* */
List<object> lstDyn = new List<object>().Add(gen);
}
}

- 522
- 5
- 13
-
Every time I can have different columns with different names and types. Can you tell me about your variant in details or show me a peace of code? – Polaris Apr 23 '10 at 13:10
If you have C# 4 you can make use of the new dynamics feature and the ExpandoObject
. You can read a tutorial about it here.

- 117,245
- 29
- 183
- 222
I am going to be looking into the ExpandoObject that was mentioned (I voted for that solution by the way as it seems easier) but yes, it is possible. I'm building a class in one of my projects where a third party utility requires a CSV line to be defined as a class.
You can build the code (I included \r\n so that I could read the resultant code):
string code = "using FileHelpers;\r\n\r\n";
code += "[DelimitedRecord(\"" + delimiter + "\")]\r\n";
code += "public class CustomCSVInputFile ";
code += "{ \r\n";
foreach (string column in columnList)
{
code += " public string " + column.Replace(" ", "") + ";\r\n";
}
code += "}\r\n";
CompilerResults compilerResults = CompileScript(code);
...
public static CompilerResults CompileScript(string source)
{
CompilerParameters parms = new CompilerParameters();
FileHelperEngine engine;
parms.GenerateExecutable = false;
parms.GenerateInMemory = true;
parms.IncludeDebugInformation = false;
string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "").Trim();
parms.ReferencedAssemblies.Add(Path.Combine(path, "FileHelpers.dll"));
CodeDomProvider compiler = CSharpCodeProvider.CreateProvider("CSharp");
return compiler.CompileAssemblyFromSource(parms, source);
}
... Like I mentioned, If I had to do it over again I would investigate the ExpandoObject but it is definitely possible to create a class from a DataTable. You would need to interrogate the column names to build your fields; my example had the list of column names provided from a "," delimited string.
My example is from a very specific use case but it should be enough to get you going should the ExpandoObject not work for you.

- 3,042
- 27
- 31