ExternalActionAttribute 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 Reflection source model if your source object declares generic methods.

When applied to a source object, this attribute references an external qualified public method to be used as a rule action. To qualify, the method must be public, static or instance, return System.Void and either be parameterless or declare only parameters of the source object type or of value types supported by Code Effects.

Syntax

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true, Inherited = true)]
public class ExternalActionAttribute : System.Attribute

Constructors

  • ExternalActionAttribute()

    Initializes a new instance of the attribute with the default values of all properties.

  • ExternalActionAttribute(Type type, string methodName)

    Initializes a new instance of the attribute that references the external action method by the type that declares it.

    Parameters

    • type (System.Type) - The type that declares the external action method.
    • methodName (System.String) - The declared name of the external action method.
  • ExternalActionAttribute(string declaringTypeOrAliasName, string methodName)

    Initializes a new instance of the attribute that references the external action method by the assembly-qualified name or the alias name of the type that declares it.

    Parameters

    • declaringTypeOrAliasName (System.String) - The assembly-qualified name or alias name of the type that declares the external action method.
    • methodName (System.String) - The declared name of the external action method.
  • ExternalActionAttribute(string assemblyName, string declaringTypeOrAliasName, string methodName)

    Deprecated. This constructor is obsolete and will be removed in a future major version. Use ExternalActionAttribute(string declaringTypeOrAliasName, string methodName) instead, passing the AssemblyQualifiedName of the type that declares the external action method.

    Initializes a new instance of the attribute.

    Parameters

    • assemblyName (System.String) - The name of the assembly that declares the type.
    • declaringTypeOrAliasName (System.String) - The name of the type.
    • methodName (System.String) - The declared name of the external action method.

Properties

  • Assembly

    Type: System.String

    Deprecated. This property is obsolete and will be removed in a future major version. Use TypeOrAliasName and set it to the AssemblyQualifiedName of the type that declares the external action method.

    Always returns null.

  • Method

    Type: System.String

    Gets the name of the method declared outside the source object that Rule Editor uses as a rule action.

  • ParamTypesToMatch

    Type: System.Type[]

    Gets or sets an array of System.Type objects that represent the parameter types matching the signature of the external method to be used as a rule action. This property can be used to select a specific overload of an external method.

  • SortOrder

    Type: System.Int16

    Gets or sets the sort order in which the rule action method appears in the action menu of the Rule Area. This property is optional. The default value is 0.

  • TestMethodName

    Type: System.String

    Gets or sets the name of the method that the Code Effects Rule Repository calls instead of the external one when a rule is tested. The test method takes the object the rule is evaluated against first, then the parameters that the external method takes from the rule; a parameter of the source object type of the external method is not repeated. Overrides a TestMethodName declared by the external method's own ActionAttribute. Ignored outside a Rule Repository test. This property is optional.

  • 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 the external method 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 (then it takes only the external method's parameters from the rule), otherwise on a new instance, which requires a public parameterless constructor. Ignored outside a Rule Repository test. This property is optional.

  • Type

    Type: System.Type

    Gets the type of the class that declares the external action method. This property is null if the attribute was created with a type name; then TypeOrAliasName is set instead.

  • TypeName

    Type: System.String

    Deprecated. This property is obsolete and will be removed in future versions. Use TypeOrAliasName instead.

    Returns the value of TypeOrAliasName.

  • TypeOrAliasName

    Type: System.String

    Gets the assembly-qualified name or the alias name of the type that declares the external action method. You can obtain the assembly-qualified name by executing typeof(ClassName).AssemblyQualifiedName. This property is null if the attribute was created with a System.Type; then Type is set instead.

Remarks

This attribute is optional and is used to reference external methods that you want to use as rule actions. An exception is thrown if the attribute references a non-qualified method. If the external method defines multiple overloads, decorate each overload that you intend to use as a rule action with the ActionAttribute, and assign a unique display name to each using its DisplayName property.

Remember that external static or instance action methods can accept your source object as a parameter. 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 external actions from the code sample below:

If ID is greater than 10 then Set new ID (0) and Set the Date to UTC

Note that the second method is displayed as parameterless (without parentheses) because it accepts a single parameter of the source type, which is omitted by the editor.

Example

using System;
using CodeEffects.Rule.Common.Attributes;

namespace TestLibrary
{
	// This source object contains no actions of its own
	// but references 2 external actions declared in the Helper class
	[ExternalAction(typeof(Helper), nameof(Helper.SetUtcDate))]
	[ExternalAction(typeof(Helper), nameof(Helper.SetNewID))]
	public class Thing
	{
		public int ID {get; set; } = 0;
		public DateTime? Date { get; set; }
	}

	public class Helper
	{
		[Action("Set new ID", "Sets a new value to the Thing's ID field")]
		public void SetNewID(Thing thing, int id)
		{
			thing.ID = id;
		}

		[Action("Set the Date to UTC", "Sets a new value to the Thing's Date field")]
		public void SetUtcDate(Thing thing)
		{
			thing.Date = DateTime.UtcNow;
		}
	}
}

Ask on Stack Overflow - Yes, it's still active
Ask in GitHub Discussions

p102

l097 --

l102

p101

×