Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic InArgument throws an exception trying to get the value

I have an Activity where I declared a InArgument without a type (because I want to know the type of the Expression at design time).

When I execute the activity I get this error in var contentTelegram line:

"The argument of type '<type>' cannot be used.  Make sure that it is declared on an activity."

Here is my code:

public InArgument Content { get; set; }


protected override PlcMessage Execute(CodeActivityContext context)
        {

            try
            {
                var contentTelegram = Content.Get(context); 

               return new PlcMessage();
            }
            catch (Exception ex)
            {

                throw;
            }


        }
like image 983
Javier Hertfelder Avatar asked Oct 04 '22 14:10

Javier Hertfelder


1 Answers

Here is what I did:

The workflow runtime needs to know about the used types in arguments, so the cacheMetadata is the key to make it work, CacheMetadata uses reflection to know about arguments, notice that only works for simple cases.

public sealed class MyActivity: CodeActivity
{
    private RuntimeArgument outMyRuntimeArgument;

    // Define an activity input argument of type string
    public OutArgument MyUntypedArgument { get; set; }


    protected override void CacheMetadata(CodeActivityMetadata metadata)
    {
        outMyArgument= new RuntimeArgument("MyUntypedArgument", MyUntypedArgument.ArgumentType, ArgumentDirection.Out);
        metadata.Bind(MyUntypedArgument, outArgument);
        metadata.AddArgument(outMyArgument);
    }     


    protected override void Execute(CodeActivityContext context)
    {
        context.SetValue(outMyRuntimeArgument, Activator.CreateInstance(Type));
    }
}
like image 179
Javier Hertfelder Avatar answered Oct 10 '22 04:10

Javier Hertfelder