Hi all,
I have started playing with Roslyn, but have gotten stuck.
(This is just a proof of concept, and I'm also not 100% sure if I am going about this the right way.)
I am trying to create a method that will calculate values based on a dynamic calculation string, by utilizing the values from a passed DataGridViewRow. At the end of the day, this must be able to run 5 calculations per DataGridViewRow (500 row in total) without taking forever.
--
I have created a simple object to carry the values:
public class calculationObject
{
public string calc { get; set;
public DataGridViewRow row { get; set; }
}
The string calculation is: (it is stored as a string in my calcutationObject):
(decimal.Parse(row.Cells["productBasePrice"].Value.ToString()) + decimal.Parse(row.Cells["productBasePrice"].Value.ToString()) ).ToString()
The methods I have created to do the calculation:
public static MetadataReference hostReference;
public static ScriptEngine engine;
public static void setup()
{
hostReference =
new AssemblyFileReference(typeof(frmImportData).Assembly.Location);
engine = new ScriptEngine(
importedNamespaces: new[] { "System", "System.Text", "System.Windows.Forms" });
}
public static string trail(calculationObject co)
{
setup();
var code = @"DataGridViewRow row = " + co.row + @";
int test = " + co.calc + @"; test.ToString();";
var session = Session.Create();
return engine.Execute<string>(code, session);
}
I call these methods as such:
calculationObject co = new calculationObject();
co.calc = cal;
co.row = dgvDatabase.Rows[0];
string test = trail(co);
--
The problem I now seem to have, is that the DataGridViewRow row are not being referenced properly by the trail method. The "code" var's value is:
DataGridViewRow row = DataGridViewRow { Index=0 };
int test = (decimal.Parse(row.Cells["productBasePrice"].Value.ToString()) + decimal.Parse(row.Cells["productBasePrice"].Value.ToString()) ).ToString(); test.ToString();
--
So I guess, what I am trying to figure out, is how to reference the col.row object in the "code" var, in order to have the calculation utilize that values.
Thank you!