ActionAttribute Class
Assembly: CodeEffects.Rule.Common.dll
Namespace: CodeEffects.Rule.Common.Attributes
Summary
IMPORTANT Generic methods with generic params cannot be used in Source XML source model as in-rule methods or rule actions. Use plain Reflection model if your source object declares generic methods.
If applied to a qualified public method of the source object, this attribute marks that method as a rule action available for use in execution-type rules. To qualify, the method must be public, static or instance, return System.Void and either be parameterless or have only parameters of the source object type or any value type supported by Code Effects.
This attribute can also be applied to a qualified method of any other public class declared in any assembly accessible to the code that evaluates the rule, making that method available as a rule action as well. In such cases, the source object must be decorated with the ExternalActionAttribute, which references the external method by its name and class type.
Syntax
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class ActionAttribute : System.Attribute
Constructors
ActionAttribute()
Initializes a new instance of the ActionAttribute class with the default values of all properties.
ActionAttribute(string displayName)
Initializes a new instance of the ActionAttribute class and sets the DisplayName property.
Parameters
- displayName (
System.String) - The label that represents the rule action in the Rule Editor.
ActionAttribute(string displayName, string description)
Initializes a new instance of the ActionAttribute class and sets the DisplayName and Description properties.
Parameters
- displayName (
System.String) - The label that represents the rule action in the Rule Editor.
- description (
System.String) - The description of the rule action.
Properties
AiAliases
Type: System.String[]
Gets or sets other names an AI agent may know this rule action by: industry terms, abbreviations, or the names other systems use (for example, "DTI" or "debt-to-income ratio"). Agents working with the Code Effects Rule Repository find the rule action by these as well as by its display name, and still call it by its display name. The aliases are never shown in the Rule Editor. They are stored with the source and sent to the AI provider the host uses, so they must hold nothing confidential. Changing them is a label change, not a new source version. This property is optional.
[Action("Send Notice", AiAliases = new[] { "notify", "alert" })]
public void SendNotice() { }
AiDescription
Type: System.String
Gets or sets what an AI agent should know about this rule action when it needs more than the Description that people read: what it means, its units, how it is used. When this property is not set, agents read the Description. It is never shown in the Rule Editor. It is stored with the source and sent to the AI provider the host uses, so it must hold nothing confidential. Changing it is a label change, not a new source version. This property is optional.
Description
Type: System.String
Gets or sets the description of the rule action method. Rule authors can view this description when they hover the mouse pointer over the action element in the Rule Area. The value of this property is ignored if Control.ShowDescriptionsOnMouseHover is set to false. It is not recommended to use this property in multilingual applications; instead, use culture-specific custom Source XML documents. This property is optional.
DisplayName
Type: System.String
Gets or sets the display name of the action method in the Rule Area. Use this property to provide a more user-friendly name for rule actions when the method name is not sufficiently descriptive for non-technical rule authors, or when multiple overloads of the same method need to appear as distinct actions. It is not recommended to use this property in multilingual applications; instead, use culture-specific custom Source XML documents. This property is optional. The default value is the name of the qualified method.
TestMethodName
Type: System.String
Gets or sets the name of the method that the Code Effects Rule Repository calls instead of this one when a rule is tested. An instance test method declared in the class of the object the rule is evaluated against (the collection item's class for a member of a collection's item type) is called on that very object and takes only the parameters this action takes from the rule. Any other test method takes that object first, then those parameters; a parameter of the source object type of this action is not repeated. Ignored outside a Rule Repository test. This property is optional.
[Action("Send Notice", TestMethodName = nameof(TestSendNotice))]
public void SendNotice(string text) { Mailer.Send(text); }
public void TestSendNotice(string text) { /* no real mail in a test */ }
TestMethodType
Type: System.Type
Gets or sets the class that declares the test method named by TestMethodName. When this property is not set, the class that declares this action is used. A static test method is called on the class; an instance one is called on the evaluated object when this is the object's class, otherwise on a new instance, which requires a public parameterless constructor. Ignored outside a Rule Repository test. This property is optional.
This attribute is optional. The Rule Editor uses any qualified method of the source object as a rule action, regardless of whether it is decorated with the ActionAttribute. The Rule Editor ignores any qualified methods that are decorated with the ExcludeFromEvaluationAttribute. If the ActionAttribute is applied to a non-qualified method, an exception is thrown. All non-qualified methods that are not decorated with the ActionAttribute are ignored.
Remember that action methods can accept your source object as a parameter. It is a useful feature for static action methods. When a rule author uses such an action in a rule, parameters of the source object type are not displayed — Rule Editor passes them automatically. In the Rule XML, the parameter of the source object type is represented as this.
The following example illustrates how the rule would appear in Rule Area when a user uses both actions from the code sample below:
If ID is greater than 10 then Rename the Thing (New Name) and Set New ID (20)
Note that the second method is displayed with only the int parameter. Your instance of the Thing type is passed to the method automatically at evaluation time; it is omitted by the editor in the Rule Area.
Example
using CodeEffects.Rule.Common.Attributes;
namespace TestLibrary
{
[Source]
public class Thing
{
public int ID {get; set; } = 0;
public string Name { get; set; }
[Action("Rename the Thing", "Gives a different name to the Thing")]
public void Rename(string newName)
{
this.Name = newName;
}
[Action("Set New ID", "Sets a new ID of the Thing")]
public static void SetID(Thing thing, int id)
{
thing.ID = id;
}
}
}