如何为加载了“AppDomain.ExecuteAssembly”的代码设置 COM 单元状态?

How can I set the COM apartment state for code loaded with `AppDomain.ExecuteAssembly`?

当我创建 Thread 时,我可以选择在启动它之前明确设置其 COM 单元状态。例如:

// using System.Threading;
var thread = new Thread(…);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

但是当我创建一个 AppDomain and load some code into it, I seem to have no explicit control over thread creation, so I have no way of calling SetApartmentState 时:

// using System;
var pluginAppDomain = AppDomain.Create("PluginAppDomain");
pluginAppDomain.ExecuteAssembly(@"Plugin.dll");

有什么方法可以指定在 AppDomain 中创建的 main/entry 线程应该使用特定的 COM 单元状态?

我知道 Plugin.dll 的主要入口方法可以用 [STAThread] or [MTAThread] 属性标记;但我们假设 Plugin.dll 没有显式声明或设置 COM 单元状态,并且我无法更改 Plugin.dll.

我重新发布 作为答案,因为它基本上回答了这个问题的大部分:

"No, creating an [app domain] does not create a thread. You are executing with the state of the thread that made the AppDomain.Create() call. Which is not good enough, you cannot uphold the STA promise. You'll need more code in the [app domain] to take care of this, the thread creation code and the Application.Run() call needs to operate in that [app domain]."