从动态加载的DLL显示表单 [英] Displaying a form from a dynamically loaded DLL

查看:178
本文介绍了从动态加载的DLL显示表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我以前问过的一个问题的扩展:这里

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

长篇小说,我动态加载一个DLL,并使一个类型使用以下代码:

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

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

从那里我可以使用类型引用几乎任何 dllTest 类中的任何内容。该类默认运行时应该提出一个表单(在这种情况下,相当空白,所以不复杂)。

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

我觉得我在这里缺少一个关键的代码行,保持窗体加载在屏幕上。

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

dllTest.cs (在DLL内)包括:

dllTest.cs (within the DLL) consists of:

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

InitializeComponent()设置表单的布局,这太长了粘贴到这里,不应该有所作为。

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

任何想法?

推荐答案

您必须使用刚刚创建的表单做一些事情:

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)

这篇关于从动态加载的DLL显示表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆