从 Dll 实例显示一个窗口

问题描述 投票:0回答:1

我正在尝试打开 Dll 中的窗口。 我创建了一个 DLL 实例,我得到了以下错误。

无法将“MyDll.CLSFormShow”类型的对象转换为类型“System.Windows.Window”。

提前致谢。

代码

string connString;
connString = "Hello World";

string strDllPath = "C:\\MyDll\\MyDll\\bin\\Debug\\netcoreapp3.1\\MyDll.dll";
string assemblyName = string.Format(strDllPath, new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName);

string strNsCn = "MyDll.CLSFormShow";
object[] paramObj = new object[1];
paramObj[0] = connString;

Assembly DLL = Assembly.LoadFrom(strDllPath);
Type classType = DLL.GetType( strNsCn);
object classInst = Activator.CreateInstance(classType, paramObj);
Window dllWinForm = (Window)classInst;
dllWinForm.ShowDialog();
type here

在执行代码时,出现错误 无法将“MyDll.CLSFormShow”类型的对象转换为类型“System.Windows.Window”。

c# dll reflection
1个回答
0
投票

您似乎正在尝试将类型为“MyDll.CLSFormShow”的对象转换为类型为“System.Windows.Window”,但它们不是兼容类型。 “CLSFormShow”类不是从“System.Windows.Window”类派生的,因此您不能将其转换为这种类型。

要打开 DLL 中的窗口,您需要使用适当的窗口类型或基类。您可以尝试将对象强制转换为窗口的基类,或者如果您知道窗口的具体类型,则可以将其强制转换为该类型。

© www.soinside.com 2019 - 2024. All rights reserved.