G
G
gleb_kudr2012-05-20 20:24:09
C++ / C#
gleb_kudr, 2012-05-20 20:24:09

Dynamic type declaration in C#

Greetings!

I didn't know that I would end up with such perversions.

I have a table Column1; column2; Column3 etc. The number of columns is dynamic by definition.

I need to make sure that all this is present in the program in the form of an array with elements that contain the names of the columns as class fields (it is important that these are fields, not an array and nothing else). For example, he gave an anonymous call of a similar thing

[] {Column1=foo1, Column2=foo2, Column3=foo3 etc...}


Attention, the question is: how to do it taking into account the dynamic nature of the table? I don't know how many columns there will be, and hardcoding 100500 fields/methods in reserve is idiotic.

For example, a conversion from a string variable to an anonymous type call would be appropriate. Or some kind of "array of methods"? My fantasy is already running out :)

In general, is it possible to do this in c#? At first glance, a very inappropriate language for this.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
ganouver, 2012-05-21
@ganouver

You can try to use class code generation based on simply listing field names with types (or even immediately based on the table structure in the database), for example, using t4 text templates .
The class template will need to be written once, in the future it will be updated during assembly.
This method does not work if the table structure changes after the program is compiled.

S
Sergey, 2012-05-20
Protko @Fesor

If the data type for your field is object, then you can safely send anything there, even if it’s a double. Information about the type of an object is saved, so you can find out what type you have at any time. Regarding the number of columns, there are already a lot of options. Need to be more specific about what exactly you want to get in the end? An example of working with data inside and out, so to speak.

G
gleb_kudr, 2012-05-20
@gleb_kudr

There is a control www.devexpress.com/Products/NET/Controls/WPF/Grid/tree.xml which only understands such a specific data representation as input. That is why I need it in such a strange form.
The code is something like this (well, a bunch of strapping around, but it doesn't matter)

//формируем коллекцию используя БД и SQL команду
TreeViewSQLiteDataCollection collection= new TreeViewSQLiteDataCollection (sqLiteCommandString, sqLiteConnection);

//это вызывается в коде TreeViewSQLiteDataCollection для того чтобы наполнить коллекцию из открытого датаридера
this.Add(new { path = sqlReader.GetString(0), parent_ID = sqlReader.GetString(1), ShowID = sqlReader.GetString(0), description = sqlReader.GetInt32(2) });

//привязываем коллекцию к контролу
treeListControl1.ItemsSource = collection;

The type of the object field is all perfectly clear and I could parse it with pleasure, only this type of field will not be displayed in the control, it understands only those types that are affected by .ToString

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question