DiceMacroAttribute Class

From DiceRoller Documentation

An attribute that can be applied to methods which denotes the method is a valid MacroCallback. The MacroRegistry.RegisterType function can be used to register types which contain methods marked with this attribute.

  • Namespace: Dice
  • Assembly: DiceRoller (in DiceRoller.dll)

Inheritance Hierarchy

System.Object

System.Attribute
Dice.DiceMacroAttribute

Syntax

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public sealed class DiceMacroAttribute : Attribute

Constructors

Name Description
DiceMacroAttribute(String) Constructs a new instance of DiceMacroAttribute

Properties

Name Description
Name Gets the name of the macro, which is not necessarily the same as the name of the method the attribute is attached to.

Remarks

This attribute can be applied to a callback to mark it as a macro. In addition to using the attribute, the type containing the decorated methods must be registered via MacroRegistry.RegisterType. This attribute should only ever be applied to public methods whose signature matches MacroCallback.

Examples

The following example demonstrates using DiceMacroAttribute to register two macros, and then registering the type containing those functions.

using System;
using System.Collections.Generic;
using Dice;

class Sample
{
    public static void Main()
    {
        Roller.DefaultConfig.MacroRegistry.RegisterType(typeof(Sample));
        var result = Roller.Roll([a]d6 + [b:2]");
        Console.WriteLine(result.Value); // retrieves the value of the expression 1d6+2
    }

    [DiceMacro("a")]
    public static void A(MacroContext context)
    {
        context.Value = 1;
    }

    [DiceMacro("b")]
    public static void B(MacroContext context)
    {
        // context.Arguments[0] is "b" and context.Arguments[1] is "2" in this example
        context.Value = Convert.ToDecimal(context.Arguments[1]);
    }
}