Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从另一个C应用程序加载在C#中创建的DLL 总结 目标_C#_Wpf - Fatal编程技术网

无法从另一个C应用程序加载在C#中创建的DLL 总结 目标

无法从另一个C应用程序加载在C#中创建的DLL 总结 目标,c#,wpf,C#,Wpf,我有两个C#项目。第一个项目的输出是一个DLL(VisualStudio中的输出类型设置为类库),它允许我从其他语言控制我的应用程序。它基本上启动我的应用程序的一个实例,而不显示任何UI窗口。我可以在其他编程语言(如Python、Matlab)中使用此DLL,而不会出现任何问题 但现在我想在另一个C#项目中使用该DLL。为此,我在项目中添加了DLL作为引用。要启动应用程序的实例,我将创建DLL中定义的类型的对象: namespace MyDLL { class Program {

我有两个C#项目。第一个项目的输出是一个DLL(VisualStudio中的输出类型设置为类库),它允许我从其他语言控制我的应用程序。它基本上启动我的应用程序的一个实例,而不显示任何UI窗口。我可以在其他编程语言(如Python、Matlab)中使用此DLL,而不会出现任何问题

但现在我想在另一个C#项目中使用该DLL。为此,我在项目中添加了DLL作为引用。要启动应用程序的实例,我将创建DLL中定义的类型的对象:

namespace MyDLL
{
    class Program
    {
        static void Main(string[] args)
        {
            MyApp app = new MyApp();
        }
    }
}
这将导致一个例外:

System.InvalidOperationException: 'The 'ResourceAssembly' property of the 'Application' type cannot be changed after it has been set.'
System.InvalidOperationException: 'The Application object is being shut down.'
at System.Windows.Application.GetResourcePackage(Uri packageUri)
at System.Windows.Application.GetResourceOrContentPart(Uri uri)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at App.MainWindow..ctor(Boolean showWindow)
at MyDLL.MyApp.startApp()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
此异常的原因是
MyApp
设置
resourcesassembly
以加载应用程序的资源(如UI图像):

我试过的 由于
Application.ResourceAssembly
只能设置一次,因此我尝试了一种解决方法,如中所述。这将防止引发ResourceAssembly异常

DLL对象尝试启动,但随后引发以下异常:

System.InvalidOperationException: 'The 'ResourceAssembly' property of the 'Application' type cannot be changed after it has been set.'
System.InvalidOperationException: 'The Application object is being shut down.'
at System.Windows.Application.GetResourcePackage(Uri packageUri)
at System.Windows.Application.GetResourceOrContentPart(Uri uri)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at App.MainWindow..ctor(Boolean showWindow)
at MyDLL.MyApp.startApp()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
此异常的InnerException值为
null
,但其源值为
PresentationFramework
。这是异常的堆栈跟踪:

System.InvalidOperationException: 'The 'ResourceAssembly' property of the 'Application' type cannot be changed after it has been set.'
System.InvalidOperationException: 'The Application object is being shut down.'
at System.Windows.Application.GetResourcePackage(Uri packageUri)
at System.Windows.Application.GetResourceOrContentPart(Uri uri)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at App.MainWindow..ctor(Boolean showWindow)
at MyDLL.MyApp.startApp()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

作为一个选项,您可以在本地包源中将类库发布为NuGet包,并在其他解决方案中将其作为常规NuGet包使用。

好的,异常状态为
应用程序对象正在关闭。
。这可能意味着您的wpf应用程序提前结束,因此我会尝试调试应用程序内部发生的事情

我注意到的一件事是,你的应用程序启动代码似乎不太正确

static void Main(string[] args)
{
    MyApp app = new MyApp();
}
假设
MyApp
是一个Wpf
Application
类,则缺少对
Run
方法所需的调用,该方法需要一个
窗口作为参数

所以我希望您的代码看起来像:

static int Main(string[] args)
{
    MyWindow window = new MyWindow();
    MyApp app = new MyApp();
    return app.Run(window);
}
另外,请注意我如何更改了
Main
的返回类型,以便我们可以返回适当的返回代码

编辑:(我显然错过了关于
资源组件的部分

您完全可以为应用程序设置资源程序集,但正如您所正确指出的,您不允许这样做两次

你第一次把它放在哪里?如果您知道以后需要将其设置为其他值,那么为什么要设置它


不幸的是,如果没有进一步的信息,我认为我们无法提供帮助。

资源程序集仅在DLL中显式设置,以将应用程序指向所需的资源(如UI中使用的图像)。我没有在尝试使用DLL的C#应用程序中显式设置资源程序集。所以我只设定了一次。是否可以应用某种默认设置,从而导致设置两次?@jasper bosch,您只能在特殊情况下手动设置资源程序集。WPF应用程序必须从非托管程序集托管,或者必须使用execute assembly以外的方法将其加载到新的应用程序域中。你是如何引用你的应用程序的?如果它只是一个DLL引用,或者您已经加载了程序集,那么它将与您的父应用程序位于同一个应用程序域中,这将不起作用。