I want to use a string as part of a variable.
For example in the code below I have a String called productLine
. I want to use the value in this string to create a variable name and to call to the property "Value" on this variable. I want to keep switching the value in 'productLine', and therefore keep switching the variable who's value method is called.
Is there a way to do this or will I need to rewrite the code and take a different approach?
foreach (string productLine in productLineData)
{
string templateKey = "{{" + productLine + "}}";
string templateValue = "";
if (productRow.productLine.Value != null)
templateValue = productRow.productLine.Value.ToString();
productRowText = productRowText.Replace(templateKey, templateValue);
}
productRow
is a model which contains the properties I wish to use.
EDIT:
productLine
contains a String value. For example it contains productName
at first. At that point I want to call productRow.productName.Value
. Next 'productLine' contains productPrice
. At that point I want to call productRow.productPrice.Value
. etc.