GetValue(T) Method

MS Activities Extensions

Collapse image Expand Image Copy image CopyHover image
Gets the value of an argument or the default value if there is no expression

Namespace: Microsoft.Activities.Extensions
Assembly: Microsoft.Activities.Extensions (in Microsoft.Activities.Extensions.dll) Version: 2.0.6.9 (2.0.6.9)

Syntax

C#
public static T GetValue<T>(
	this ActivityContext context,
	InArgument<T> argument,
	T defaultValue
)
Visual Basic
<ExtensionAttribute> _
Public Shared Function GetValue(Of T) ( _
	context As ActivityContext, _
	argument As InArgument(Of T), _
	defaultValue As T _
) As T
Visual C++
public:
[ExtensionAttribute]
generic<typename T>
static T GetValue(
	ActivityContext^ context, 
	InArgument<T>^ argument, 
	T defaultValue
)

Parameters

context
Type: System.Activities..::..ActivityContext
The context.
argument
Type: System.Activities..::..InArgument<(Of <(<'T>)>)>
The argument.
defaultValue
Type: T
The default value.

Type Parameters

T
The type of the argument

Return Value

the value of an argument or the default value if there is no expression

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type ActivityContext. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

Default values are not supported on InOutArguments because we cannot tell if the value was set by your code or if it is simply the default(T) because no value was set

Examples

An activity that has an optional in argument
CopyC#
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ActivityWithOptionalArgsContext.cs" company="Microsoft">
//   Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System.Activities;
using System.ComponentModel;

using Microsoft.Activities.Extensions;

/// <summary>
/// An activity with optional args and context
/// </summary>
// ReSharper disable CheckNamespace
public sealed class ActivityWithOptionalArgsContext : CodeActivity<string>
{
    // ReSharper restore CheckNamespace
    #region Constants

    /// <summary>
    /// The default optional value
    /// </summary>
    public const int DefaultOptionalValue = 1;

    #endregion

    // Prevent serialization of null value
    #region Public Properties

    /// <summary>
    /// Gets or sets OptionalArg.
    /// </summary>
    [DefaultValue(null)]
    public InArgument<int> OptionalArg { get; set; }

    /// <summary>
    /// Gets or sets RequiredArg.
    /// </summary>
    [RequiredArgument]
    public InArgument<string> RequiredArg { get; set; }

    #endregion

    #region Methods

    /// <summary>
    /// When implemented in a derived class, performs the execution of the activity.
    /// </summary>
    /// <returns>
    /// The result of the activity’s execution.
    /// </returns>
    /// <param name="context">The execution context under which the activity executes.</param>
    protected override string Execute(CodeActivityContext context)
    {
        var num = context.GetValue(this.OptionalArg, DefaultOptionalValue);

        return string.Format("{0}: {1}", this.RequiredArg.Get(context), num);
    }

    #endregion
}

See Also