ComparisonEvaluationResult
Contents
[
Hide
]ComparisonEvaluationResult class
The comparison evaluation result.
To learn more, visit the Working with Fields documentation article.
public sealed class ComparisonEvaluationResult
Constructors
Name | Description |
---|---|
ComparisonEvaluationResult(bool) | Creates a comparison evaluation result. |
ComparisonEvaluationResult(string) | Creates a failed comparison evaluation result with the corresponding error message. |
Properties
Name | Description |
---|---|
ErrorMessage { get; } | Gets the failed comparison evaluation result’s error message. |
Result { get; } | Gets the comparison evaluation result. |
Examples
Shows how to implement custom evaluation for the IF and COMPARE fields.
public void ConditionEvaluationExtensionPoint(string fieldCode, sbyte comparisonResult, string comparisonError,
string expectedResult)
{
const string left = "\"left expression\"";
const string @operator = "<>";
const string right = "\"right expression\"";
DocumentBuilder builder = new DocumentBuilder();
// Field codes that we use in this example:
// 1. " IF {0} {1} {2} \"true argument\" \"false argument\" ".
// 2. " COMPARE {0} {1} {2} ".
Field field = builder.InsertField(string.Format(fieldCode, left, @operator, right), null);
// If the "comparisonResult" is undefined, we create "ComparisonEvaluationResult" with string, instead of bool.
ComparisonEvaluationResult result = comparisonResult != -1
? new ComparisonEvaluationResult(comparisonResult == 1)
: comparisonError != null ? new ComparisonEvaluationResult(comparisonError) : null;
ComparisonExpressionEvaluator evaluator = new ComparisonExpressionEvaluator(result);
builder.Document.FieldOptions.ComparisonExpressionEvaluator = evaluator;
builder.Document.UpdateFields();
Assert.AreEqual(expectedResult, field.Result);
evaluator.AssertInvocationsCount(1).AssertInvocationArguments(0, left, @operator, right);
}
/// <summary>
/// Comparison expressions evaluation for the FieldIf and FieldCompare.
/// </summary>
private class ComparisonExpressionEvaluator : IComparisonExpressionEvaluator
{
public ComparisonExpressionEvaluator(ComparisonEvaluationResult result)
{
mResult = result;
if (mResult != null)
{
Console.WriteLine(mResult.ErrorMessage);
Console.WriteLine(mResult.Result);
}
}
public ComparisonEvaluationResult Evaluate(Field field, ComparisonExpression expression)
{
mInvocations.Add(new[]
{
expression.LeftExpression,
expression.ComparisonOperator,
expression.RightExpression
});
return mResult;
}
public ComparisonExpressionEvaluator AssertInvocationsCount(int expected)
{
Assert.AreEqual(expected, mInvocations.Count);
return this;
}
public ComparisonExpressionEvaluator AssertInvocationArguments(
int invocationIndex,
string expectedLeftExpression,
string expectedComparisonOperator,
string expectedRightExpression)
{
string[] arguments = mInvocations[invocationIndex];
Assert.AreEqual(expectedLeftExpression, arguments[0]);
Assert.AreEqual(expectedComparisonOperator, arguments[1]);
Assert.AreEqual(expectedRightExpression, arguments[2]);
return this;
}
private readonly ComparisonEvaluationResult mResult;
private readonly List<string[]> mInvocations = new List<string[]>();
}
See Also
- namespace Aspose.Words.Fields
- assembly Aspose.Words