Extension Method Support

In .NET Framework 3.5, extension methods can be invoked from Workflow rules and declarative activity conditions. Extension methods are methods that can be invoked using instance method syntax. In effect, extension methods make it possible to extend existing types and constructed types with additional methods.

In C#, extension methods are declared by specifying the keyword this as a modifier on the first parameter of methods declared in a static class. In Visual Basic, extension methods are declared in a Module by creating a Sub or Function that is marked with the Extension attribute. In both C# and Visual Basic extension methods, the first parameter designates the type that is being extended. The following is an example of an extension to the String class.

public static class ExtensionMethods
{
    public static void WriteLine(this string value)
    {
        Console.WriteLine(value);
    }

    public static bool HasValue(this string value)
    {
        return value.Length > 0;
    }
}

The following code example shows how extension methods can be invoked. In this example, the extension method WriteLine that was previously declared on String is being invoked from code.

string s = "Hello World!";
s.WriteLine();

Extensions can also be invoked from a rule. The following example shows how to invoke the HasValue and WriteLine extension methods on a workflow member of type String named Msg from a rule.

IF this.Msg.HasValue()
THEN this.Msg.WriteLine()

Extension methods can also be invoked from a declarative activity condition. The following example shows how to invoke the HasValue extension method from a declarative activity condition.

this.Msg.HasLength()

In Workflow, validation for extension methods is done at compile time. In C#, if the extension method is not defined in a static class in your solution, then the validator searches through all of the referenced assemblies listed in your solution to resolve your extension method call. In Visual Basic, the extension method must be declared in an external assembly. In both C# and Visual Basic, only public extension methods can be invoked from a rule.

Note

If you try to use any code relying on this functionality with the .NET Framework 3.0, you get a validation error because the method you called is not defined on that class.

For more information about extension methods in C#, see the C# Version 3.0 Specification and Extension Methods (C# Programming Guide). For more information about extension methods in Visual Basic, see Extension Methods (Visual Basic).

See Also

Other Resources

Rule Changes in .NET Framework 3.5
Order Processing Policy