C# 从动态加载的 DLL 中显示表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1088348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:56:03  来源:igfitidea点击:

Displaying a form from a dynamically loaded DLL

c#dllforms

提问by scrot

This is an extension of a question I previously asked here.

这是我之前在这里提出的问题的延伸。

Long story short, I dynamically load a DLL and make a typeout of it with the following code:

长话短说,我动态加载一个 DLL 并type使用以下代码对其进行制作:

Assembly assembly = Assembly.LoadFile("C:\test.dll");
Type type = assembly.GetType("test.dllTest");
Activator.CreateInstance(type);

From there I can use typeto reference virtually anything in the dllTestclass. The class by default when ran should bring up a form (in this case, fairly blank, so it's not complex).

从那里我可以type用来引用dllTest类中的几乎任何东西。默认情况下,该类在运行时应该显示一个表单(在这种情况下,相当空白,所以它并不复杂)。

I feel like I'm missing a key line of code here that's keeping the form from loading on the screen.

我觉得我在这里遗漏了一行关键代码,它使表单无法加载到屏幕上。

dllTest.cs(within the DLL) consists of:

dllTest.cs(在 DLL 内)包括:

namespace test
{
    public partial class dllTest : Form
    {
        public dllTest()
        {
            InitializeComponent();
        }
    }
}

InitializeComponent()sets up the layout of the form, which is far too long to paste here and shouldn't make a difference.

InitializeComponent()设置表单的布局,粘贴在这里太长了,不应该有什么不同。

Any ideas?

有任何想法吗?

采纳答案by Juanma

You have to do something with the form you've just created:

你必须对你刚刚创建的表单做一些事情:

Assembly assembly = Assembly.LoadFile("C:\test.dll");
Type type = assembly.GetType("test.dllTest");
Form form = (Form)Activator.CreateInstance(type);
form.ShowDialog(); // Or Application.Run(form)

回答by Quintin Robinson

Yes, you aren't actually specifying any code to run outside the class initializer. For instance, with forms you have to actually show them.

是的,您实际上并未指定在类初始值设定项之外运行的任何代码。例如,对于表格,您必须实际展示它们。

You could modify your code to the following...

您可以将代码修改为以下...

Assembly assembly = Assembly.LoadFile("C:\test.dll");
Type type = assembly.GetType("test.dllTest");
Form form = Activator.CreateInstance(type) as Form;
form.ShowDialog();

回答by Timothy Carter

I would go with:

我会去:

Assembly assembly = Assembly.LoadFile("C:\test.dll");
Type type = assembly.GetType("test.dllTest");
object obj = Activator.CreateInstance(type);
Form form = obj as Form;
if (form != null)
    form.Show(); //or ShowDilaog() whichever is needed

Other error checking/handling should be added; however at the very least I would ensure the conversion works.

应添加其他错误检查/处理;但是至少我会确保转换有效。

回答by Uday

If a class belongs to Formthen the Assembly.GetType()returns NULL. If a class belongs to User Controlthen I can see that the type is returned.

如果一个类属于Form然后Assembly.GetType()返回NULL。如果一个类属于User Control然后我可以看到该类型被返回。

Also the syntax should be as:

语法也应该是:

Type type = assembly.GetType("Assemblytest.clsTest");

where

在哪里

  • clsTestwill be the name of class (of a user control)
  • Assemblytestis the name of assembly without the .dll extention.
  • clsTest将是类的名称(用户控件的)
  • Assemblytest是不带 .dll 扩展名的程序集名称。