如何创建一个新的InArgument <t>,其中T可以动态更改</t>

时间:2011-11-23 20:35:43

标签: c# workflow-foundation workflow-foundation-4

在重新托管WorkflowDesigner中,我想通过以下代码为InArguments分配一个valiable:

 Type inActivityType = leftActivity.GetType();
 PropertyInfo inPropInfo = inActivityType.GetProperty(inArgumentName);
 InPropInfo.SetValue(leftActivity, 
                     new InArgument<someType>(new VisualBasicValue<someType>(variableName)), 
                     null);

由于“someType”动态分配了不同的类型,因此创建新InArgument的常规方法将无法正常工作。我找到的一个想法是:

//  Type theType - the value of theType will be assigned dynamicly, 
//  base on different InArgument be selected

 Type InArgType = typeof(InArgument<>).MakeGenericType(new[] { theType });
 object InArg = Activator.CreateInstance(InArgType);
.  .  .
  InPropInfo.SetValue(leftActivity, 
                     InArg, 
                     null);

但问题是,我只能创建一个新的InArgument,但不能为它分配新的VisualBasicValue(variableName)。

感谢您的任何想法。

1 个答案:

答案 0 :(得分:1)

您也可以使用相同的技巧创建VisualBasicValue的新实例,并使用带有构造函数参数的overload of CreateInstance将其传递给InArgument。也许是这样的:

Type InArgType = typeof(InArgument<>).MakeGenericType(new[] { theType });
Type vbValueType = typeof (VisualBasicValue<>).MakeGenericType(new[] {theType});
object vbValue = Activator.CreateInstance(vbValueType, variableName);
object InArg = Activator.CreateInstance(InArgType, vbValue); 

但是,根据您要做的事情,您可以使用非通用InArgument而不用担心那里的泛型。