locked
Runtime creation of InArgument<T> RRS feed

  • Question

  • How can I do the equivalent of:

    var MyType = typeof(InArgument<T>); 
    
    

    when T is known at run time, not compile time?


    Chuck Berg | Consulting Engineer | Corporate IT - Station Casinos, Inc.
    Thursday, March 4, 2010 9:30 PM

Answers

  • Yes, this is what I was looking for.  I was expecting to find a Create() method on InArgument, and didn't think to look for a static method in its base class.

    So the code that ends up doing exactly what I want is:

    ab.Properties.Add(new DynamicActivityProperty()
    {
        Name = "IncomingMessage",
        Type = Argument.Create(ChosenType, ArgumentDirection.In).GetType(),
        Attributes = { new RequiredArgumentAttribute() }
    });
    
    Thanks everyone for helping out.


    Chuck Berg | Consulting Engineer | Corporate IT - Station Casinos, Inc.
    Friday, March 5, 2010 8:44 PM

All replies

  • Hi Chuck,

    If you are trying to do this at the workflow level, could you use the assign activity to associate variable1 (which is defined as a variable of type Type) to argument1.GetType() (where argument1 is your in argument)? 

    Thanks,
    Eric
    Thursday, March 4, 2010 11:21 PM
  • Eric,

    Once again I failed to communicate enough of the context around my question.  No, I am not trying to do this at the workflow level, I'm trying to do this in the host application that is setting up to create and/or execute a workflow.

    Specifically, I am limiting the choices my user has for input arguments, and then dynamically creating those arguments based on his selections.  Here is my existing code prior to working on this change.

                    ActivityBuilder ab = new ActivityBuilder();
                    ab.Implementation = new Sequence();
                    ab.Properties.Add(new DynamicActivityProperty()
                    {
                        Name = "ApplicationName",
                        Type = typeof(InArgument<string>),
                        Attributes = { new RequiredArgumentAttribute() }
                    });
                    ab.Properties.Add(new DynamicActivityProperty()
                    {
                        Name = "IncomingMessage",
                        Type = typeof(InArgument<object>),
                        Attributes = { new RequiredArgumentAttribute() }
                    });
    Instead of <object> in the second declaration of InArgument, I want to place the type just chosen by the user.  But of course, that makes it a dynamic rather than a static declaration.  Is there a way to do this?


    Chuck Berg | Consulting Engineer | Corporate IT - Station Casinos, Inc.
    Friday, March 5, 2010 12:20 AM
  • My existing code works just fine for what it does.  The point of my question is that a new requirement I'm trying to meet is that <object> needs to be specified at runtime as a more specific Type that is specified by user selection from a list.


    Chuck Berg | Consulting Engineer | Corporate IT - Station Casinos, Inc.
    Friday, March 5, 2010 12:32 AM
  • Hmmm, we're not communicating very well, so let me try again.

    I don't want my user changing the argument type with the built-in argument tools because that gives him more control over what type he chooses than I want him to have.  I am hiding the argument tool with WorkflowShellBarItemVisibility so he has no control over arguments at all.

    Instead, when he creates a new workflow, I am presenting a list of the available types he can choose from.  Then I want to create an InArugment<> of the type he has chosen.  So I need a runtime method for creating an InArgument<> of a specific type.

    Is that more clear?


    Chuck Berg | Consulting Engineer | Corporate IT - Station Casinos, Inc.
    Friday, March 5, 2010 1:03 AM
  • ok, so are you planning to create the argument when you executing the designer? you can try this way

    after user finishes edit on the designer,
    convert your whole activity to dynamic activity, and add dynamic property for InArgument with the type user selected.

    http://msdn.microsoft.com/en-us/library/dd807392(VS.100).aspx

    Friday, March 5, 2010 1:32 AM
  • I didn't really follow the whole conversation but this bit

    >I want to create an InArugment<> of the type he has chosen.  So I need a runtime method for creating an InArgument<> of a specific type.

    this makes me think of

    var argument = Argument.Create(type, direction)

    Tim
    Friday, March 5, 2010 1:45 AM
  • if you plan to create the argument when you executing the designer, you may use the DynamicActivity I posted above.
    if you want to create the argument as soon as user selected the type, I don't understand why you don't use dynamicProperty to create the argument and add it to ActivityBuilder directly?

    maybe I still misunderstand something?
    Friday, March 5, 2010 2:57 AM
  • Yes, this is what I was looking for.  I was expecting to find a Create() method on InArgument, and didn't think to look for a static method in its base class.

    So the code that ends up doing exactly what I want is:

    ab.Properties.Add(new DynamicActivityProperty()
    {
        Name = "IncomingMessage",
        Type = Argument.Create(ChosenType, ArgumentDirection.In).GetType(),
        Attributes = { new RequiredArgumentAttribute() }
    });
    
    Thanks everyone for helping out.


    Chuck Berg | Consulting Engineer | Corporate IT - Station Casinos, Inc.
    Friday, March 5, 2010 8:44 PM