Hi,
public partial class Form1 : Form
{
private Type type;
private Type typeItems;
private Type typeDataSource;
Public form1()
{
typeDataSource = typeof(PremieTabel);
typeItems = PremieTabel.Items.GetType();
codeTabel = "PremieTabel";
dataGridView2.DataSource = PremieTabel.Items;
lblTitel.Text = codeTabel;
......
}
public btn-clickevent(object sender, EventArgs e)
{
// Export data to csv file
FieldInfo[] fields = typeDataSource.GetFields();
PropertyInfo[] properties = typeDataSource.GetProperties();
}
Starting the program typeDataSource, typeItems are filled up with the Items of object PremieTabel. Also the datagrid is filled up correctly.
But we won't use reflection ( because I have the same code a lot but with the items of a different object).
In runtime I push button btn. I want to know which datasource is displayed on screen (in this case PremieTabel.Items) and get the contents
of it so I can export the contents to a csv. file.
I can check which type is in scope, but who can I get teh data of that object.
Thanks
Loading
NainilPosted Feb 17, 2010, 12:45 PM
To get the data in the object, you will need the instance of the object from which you will get the data. You are only quering the type of the object to get the properties and fields. It will only return the properties and fields the class has. To get the value of the property, you will have to first obtain the reference to the object (in your case, the PremieTable object instance) and then you can use that instance and get the Items property values. Assuming that the PremieTabel has the first property called "Items", you can do something like this in the btn-clickevent:
Type typeDataSource = typeof(PremieTabel);
FieldInfo[] fields = typeDataSource.GetFields();
PropertyInfo[] properties = typeDataSource.GetProperties();
PremieTabel oPremieTable = PremieTabel; // oPremieTable is an instance of the type PremieTabel
List<SqlParameter> oList = properties[0].GetValue(oPremieTabel, null) as List<SqlParameter>;
The above code assumes that the first property called "Items" is of type List