显示来自DLL实例的窗口

huangapple go评论65阅读模式
英文:

Show a Window from the instance of a Dll

问题

我正在尝试打开位于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'。

英文:

I am trying to open the window which is inside a Dll.
I created an instance of the DLL and I and getting the below error.

Unable to cast object of type 'MyDll.CLSFormShow' to type 'System.Windows.Window'.

Thanks in advance.

Code

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

On executing the code, I am getting the error
Unable to cast object of type 'MyDll.CLSFormShow' to type 'System.Windows.Window'.

答案1

得分: 0

It seems like you are trying to cast an object of type 'MyDll.CLSFormShow' to type 'System.Windows.Window', but they are not compatible types. The 'CLSFormShow' class is not derived from the 'System.Windows.Window' class, so you cannot cast it to this type.

To open the window that is inside the DLL, you need to use the appropriate type or base class of the window. You can try casting the object to the base class of the window, or if you know the specific type of the window, you can cast it to that type.

// create an instance of the DLL
MyDll.CLSFormShow myForm = new MyDll.CLSFormShow();

// check if the object is of the expected type
if (myForm is System.Windows.Window)
{
    // cast the object to the Window type and open the window
    System.Windows.Window myWindow = (System.Windows.Window)myForm;
    myWindow.ShowDialog();
}
else
{
    // handle the error condition
    Console.WriteLine("Error: object is not a Window");
}
英文:

It seems like you are trying to cast an object of type 'MyDll.CLSFormShow' to type 'System.Windows.Window', but they are not compatible types. The 'CLSFormShow' class is not derived from the 'System.Windows.Window' class, so you cannot cast it to this type.

To open the window that is inside the DLL, you need to use the appropriate type or base class of the window. You can try casting the object to the base class of the window, or if you know the specific type of the window, you can cast it to that type.

// create an instance of the DLL
MyDll.CLSFormShow myForm = new MyDll.CLSFormShow();

// check if the object is of the expected type
if (myForm is System.Windows.Window)
{
    // cast the object to the Window type and open the window
    System.Windows.Window myWindow = (System.Windows.Window)myForm;
    myWindow.ShowDialog();
}
else
{
    // handle the error condition
    Console.WriteLine("Error: object is not a Window");
}

huangapple
  • 本文由 发表于 2023年3月1日 15:14:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定