C# 如何在.NET中的工作流活动中使用外部类?
我在类库中定义了一个类:C# 如何在.NET中的工作流活动中使用外部类?,c#,types,workflow-foundation,class-library,C#,Types,Workflow Foundation,Class Library,我在类库中定义了一个类: namespace TestLib { public class TestClass { public static void Test() { } } } 我还在XAML文件中定义了一个Activity。现在我想在该活动中使用InvokeMethodactivity调用类中的静态方法,但调试器抛出运行时异常,该异常表示: “无法加载文件或程序集'TestLib,PublicKeyToken=null'或其依赖项之一。系统找不到
namespace TestLib
{
public class TestClass
{
public static void Test() { }
}
}
我还在XAML
文件中定义了一个Activity
。现在我想在该活动中使用InvokeMethod
activity调用类中的静态方法,但调试器抛出运行时异常,该异常表示:
“无法加载文件或程序集'TestLib,PublicKeyToken=null'或其依赖项之一。系统找不到指定的文件。”
类库被正确引用,我也可以在XAML源代码中看到这一点。
如果我将这个类包含到源代码中,它就可以正常工作
在工作流中使用外部类型的正确方法是什么?我做错了什么?谢谢
更新:
重新创建此案例非常简单
创建一个名为TestLib
的类库
项目,并添加上面的TestClass
创建一个活动库
项目,并将主活动重命名为测试活动
。添加对TestLib
的引用。添加一个包含InvokeMethod
活动的Sequence
活动,并使用它调用方法TestClass.Test
。这是XAML
code:
<Activity mc:Ignorable="sap sap2010 sads" x:Class="WorkflowSimpleTest.TestActivity" sap2010:ExpressionActivityEditor.ExpressionActivityEditor="C#" sap2010:WorkflowViewState.IdRef="WorkflowSimpleTest.Activity1_1"
xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
xmlns:t="clr-namespace:TestLib;assembly=TestLib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextExpression.NamespacesForImplementation>
<sco:Collection x:TypeArguments="x:String">
<x:String>System</x:String>
<x:String>System.Collections.Generic</x:String>
<x:String>System.Data</x:String>
<x:String>System.Linq</x:String>
<x:String>System.Text</x:String>
</sco:Collection>
</TextExpression.NamespacesForImplementation>
<TextExpression.ReferencesForImplementation>
<sco:Collection x:TypeArguments="AssemblyReference">
<AssemblyReference>Microsoft.CSharp</AssemblyReference>
<AssemblyReference>System</AssemblyReference>
<AssemblyReference>System.Activities</AssemblyReference>
<AssemblyReference>System.Core</AssemblyReference>
<AssemblyReference>System.Data</AssemblyReference>
<AssemblyReference>System.Runtime.Serialization</AssemblyReference>
<AssemblyReference>System.ServiceModel</AssemblyReference>
<AssemblyReference>System.ServiceModel.Activities</AssemblyReference>
<AssemblyReference>System.Xaml</AssemblyReference>
<AssemblyReference>System.Xml</AssemblyReference>
<AssemblyReference>System.Xml.Linq</AssemblyReference>
<AssemblyReference>mscorlib</AssemblyReference>
<AssemblyReference>TestLib</AssemblyReference>
<AssemblyReference>WorkflowSimpleTest</AssemblyReference>
</sco:Collection>
</TextExpression.ReferencesForImplementation>
<Sequence sap2010:WorkflowViewState.IdRef="Sequence_1">
<InvokeMethod sap2010:WorkflowViewState.IdRef="InvokeMethod_1" MethodName="Test" TargetType="t:TestClass" />
<WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_1" Text="End activity." />
<sads:DebugSymbol.Symbol>d1dEOlxFbHZlZGluXFByb2plY3RzXEF1dG9tYXRpb24mQklcc291cmNlXFNFQ1VSSVRBU1xXb3JrZm9sd1NpbXBsZVRlc3RcVGVzdEFjdGl2aXR5LnhhbWwEJgMqDgIBAScFJ3ECAQQoBShVAgECKEMoUgIBAw==</sads:DebugSymbol.Symbol>
</Sequence>
<sap2010:WorkflowViewState.ViewStateManager>
<sap2010:ViewStateManager>
<sap2010:ViewStateData Id="InvokeMethod_1" sap:VirtualizedContainerService.HintSize="218,130" />
<sap2010:ViewStateData Id="WriteLine_1" sap:VirtualizedContainerService.HintSize="218,62" />
<sap2010:ViewStateData Id="Sequence_1" sap:VirtualizedContainerService.HintSize="240,356">
<sap:WorkflowViewStateService.ViewState>
<scg:Dictionary x:TypeArguments="x:String, x:Object">
<x:Boolean x:Key="IsExpanded">True</x:Boolean>
</scg:Dictionary>
</sap:WorkflowViewStateService.ViewState>
</sap2010:ViewStateData>
<sap2010:ViewStateData Id="WorkfolwSimpleTest.Activity1_1" sap:VirtualizedContainerService.HintSize="280,436" />
</sap2010:ViewStateManager>
</sap2010:WorkflowViewState.ViewStateManager>
</Activity>
系统
System.Collections.Generic
系统数据
系统Linq
系统文本
Microsoft.CSharp
系统
系统.活动
系统核心
系统数据
System.Runtime.Serialization
System.ServiceModel
System.ServiceModel.Activities
System.Xaml
命名空间
System.Xml.Linq
mscorlib
测试库
WorkflowSimpleTest
D1DEOLXFBHZLZGLUXFBYB2PLY3RZXEF1DG9TYXRPB24MQKLCC291CMNLXFNFQ1VSSVRBU1XXB3JRZM9SD1NPBXBSZVRLC3RCVGVZDEFJDGL2AXR5LNHHBWWEJGMQODGIBASCFJ3ECAQQOBSHVAGECKEMOUGIBAW==
真的
创建控制台应用程序并使用以下代码执行工作流:
Dictionary<string, object> vars = new Dictionary<string, object>() { };
WorkflowApplication wfApp = new WorkflowApplication(new TestActivity(), vars);
wfApp.Completed += delegate (WorkflowApplicationCompletedEventArgs e)
{
Console.WriteLine("Completed.");
};
wfApp.Idle += delegate (WorkflowApplicationIdleEventArgs e1)
{
Console.WriteLine("Idle.");
};
wfApp.Run();
Dictionary vars=newdictionary(){};
WorkflowApplication wfApp=新的WorkflowApplication(NewTestActivity(),vars);
wfApp.Completed+=委托(WorkflowApplicationCompletedEventArgs e)
{
Console.WriteLine(“已完成”);
};
wfApp.Idle+=委托(WorkflowApplicationIdleEventArgs e1)
{
控制台。写线(“空闲”);
};
wfApp.Run();
应用程序应引发运行时异常。错误与声明完全一致。显然,通过在
工作流库
项目中添加对库的引用,我也应该在宿主项目中添加一个引用。是否可能添加更多的代码,可能是使用调用方法
和其他重要内容?@J.Pichardo我更新了我的问题。