Want to use Reflextion if it possible in my case

T

tony

Hello!!

I have the method CollectData that I want to use Reflection on.
I use foreach looping through a collection of DSRow. This DSRow does not
have a property but I can add one if it would solve my problem.
After each lap in the foreach loop I instansiate a Composition object.
In the switch case part I have these statements
case "EAF" :
case "C_min" :
case "C_aim" :
case "C_max" :
case "P" :
case "C" :
case "V" :

meaning that if I come here I have any of these in variable colName "EAF",
"C_min", "C_aim", "C_max", "P,"C" or "V"

In the comp class I also have properties with exact these names "EAF",
"C_min", "C_aim", "C_max", "P,"C" or "V"

What I want if it's possible it to use reflection to update the comp object
by using the properties "EAF", "C_min", "C_aim", "C_max", "P,"C" or "V"
The value to set in these properties is (double)gridRow[j];

I have written something here but I know this is wrong but you out there
might know how I should write instead.
comp.GetType().GetProperty(Value).SetValue(comp,
(double)comp.GetType().GetProperty(colName).GetValue(comp, null), null);

public bool CollectData(ref IDialogPostData post)
{
...
foreach( DSRow gridRow in (ArrayList)m_flgLimit.Rows.DataSource )
{
MeltPracDataComposition.Composition comp = new
MeltPracDataComposition.Composition();

for (int j = 0; j < m_flgLimit.Columns.Items.Count; j++)
{
string colName = m_flgLimit.Columns.Items[j].Caption;

switch( colName )
{
case "EAF" :
case "C_min" :
case "C_aim" :
case "C_max" :
case "P" :
case "C" :
case "V" :
//

comp.GetType().GetProperty(Value).SetValue(comp,
(double)comp.GetType().GetProperty(colName).GetValue(comp, null), null);
}// end switch
}//end for
...
}//end foreach
...
}

//Tony
 
A

Andreas Mueller

tony said:
Hello!!

I have the method CollectData that I want to use Reflection on.
I use foreach looping through a collection of DSRow. This DSRow does not
have a property but I can add one if it would solve my problem.
After each lap in the foreach loop I instansiate a Composition object.
In the switch case part I have these statements
case "EAF" :
case "C_min" :
case "C_aim" :
case "C_max" :
case "P" :
case "C" :
case "V" :

meaning that if I come here I have any of these in variable colName "EAF",
"C_min", "C_aim", "C_max", "P,"C" or "V"

In the comp class I also have properties with exact these names "EAF",
"C_min", "C_aim", "C_max", "P,"C" or "V"

What I want if it's possible it to use reflection to update the comp object
by using the properties "EAF", "C_min", "C_aim", "C_max", "P,"C" or "V"
The value to set in these properties is (double)gridRow[j];

I have written something here but I know this is wrong but you out there
might know how I should write instead.
comp.GetType().GetProperty(Value).SetValue(comp,
(double)comp.GetType().GetProperty(colName).GetValue(comp, null), null);

public bool CollectData(ref IDialogPostData post)
{
...
foreach( DSRow gridRow in (ArrayList)m_flgLimit.Rows.DataSource )
{
MeltPracDataComposition.Composition comp = new
MeltPracDataComposition.Composition();

for (int j = 0; j < m_flgLimit.Columns.Items.Count; j++)
{
string colName = m_flgLimit.Columns.Items[j].Caption;

switch( colName )
{
case "EAF" :
case "C_min" :
case "C_aim" :
case "C_max" :
case "P" :
case "C" :
case "V" :
//

comp.GetType().GetProperty(Value).SetValue(comp,
(double)comp.GetType().GetProperty(colName).GetValue(comp, null), null);
}// end switch
}//end for
...
}//end foreach
...
}

//Tony

Hi Tony,

from reading your code this should work:

// get the prop info for the property that matches the
// column
PropertyInfo pi = comp.GetType().GetProperty(colName);

// set the value on the Composition object
double val = (double)gridRow[j];
pi.SetValue(comp, val, null)

HTH,
Andy
 
Top