I need some help in understanding about Roslyn (i am new about that technology). In my scenario i have a set of rule, each rule is a string that contain the text of a lambda expression; each lambda take an object as input and get a string as output.
Rule example :
obj => obj.ToString()
My goal is to compile each lambda once (at program startup) and then execute it with the different parameter i get (a runtime) each time.
So from what i have read i think i need to start compiling the lambda first :
foreach ( var rule in rules )
{
//_formattingRules -> a dictionary that will hold all compiled lambda
if ( string.IsNullOrEmpty(rule) == false && _formattingRules.ContainsKey ( rule ) == false )
{
try
{
var currentOption = ScriptOptions.Default.AddReferences(property.PropertyType.Assembly)
...other refereces;
var script = CSharpScript.Create<Func<object, string>>(rule, options: currentOption);
_formattingRules.Add(rule, script);
}
catch (Exception err)
{
//error handling
}
}
}
Then when i need to use a specific rule on object :
if (_formattingRules.ContainsKey(rule) == true)
{
var currentRule = _formattingRules[rule];
if (currentRule != null)
{
string formattedResult = currentRule.DynamicInvoke(new object [] {objectToFormat});
}
}
The problem is that when i do so, the DynamicInvoke throw a TargetParameterCountException and that let me think i am invoking the wrong thing. So i belive there is something in my understanding of how Roslyn work that is faulty, and i would like help to figure it out how to achieve a compilation first (once) with multiple execution (different parameter) of a lambda expression.
Aucun commentaire:
Enregistrer un commentaire