C# 如何创建新的Inagument<;T>;其中T可以动态更改

C# 如何创建新的Inagument<;T>;其中T可以动态更改,c#,workflow-foundation,workflow-foundation-4,C#,Workflow Foundation,Workflow Foundation 4,在rehost WorkflowDesigner中,我希望通过以下代码为InArguments分配一个有效值: Type inActivityType = leftActivity.GetType(); PropertyInfo inPropInfo = inActivityType.GetProperty(inArgumentName); InPropInfo.SetValue(leftActivity, new InArgument<s

在rehost WorkflowDesigner中,我希望通过以下代码为InArguments分配一个有效值:

 Type inActivityType = leftActivity.GetType();
 PropertyInfo inPropInfo = inActivityType.GetProperty(inArgumentName);
 InPropInfo.SetValue(leftActivity, 
                     new InArgument<someType>(new VisualBasicValue<someType>(variableName)), 
                     null);
Type inActivityType=leftActivity.GetType();
PropertyInfo inPropInfo=inActivityType.GetProperty(inArgumentName);
InPropInfo.SetValue(leftActivity,
新的InArgument(新的VisualBasicValue(variableName)),
无效);
由于“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);
//键入类型-将动态分配类型的值,
//根据不同的环境选择不同的环境
Type InArgType=typeof(inagrament).MakeGenericType(new[]{theType});
对象InArg=Activator.CreateInstance(InArgType);
.  .  .
InPropInfo.SetValue(leftActivity,
伊纳格,
无效);
但问题是,我只能创建一个新的inagramment,但不能为其分配新的VisualBasicValue(variableName)


谢谢你的建议。

你也可以用同样的技巧创建一个新的
VisualBasicValue
实例,并使用带构造函数参数的。也许是这样的:

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); 
Type InArgType=typeof(InArgument).MakeGenericType(new[]{theType});
类型vbValueType=typeof(VisualBasicValue).MakeGenericType(新[]{theType});
对象vbValue=Activator.CreateInstance(vbValueType,variableName);
对象InArg=Activator.CreateInstance(InArgType,vbValue);

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

非常感谢。在最后一行代码中,我们还需要传递vbValue,对吗?+1用于删除泛型。通过这样做,你就失去了泛型的所有好处。