如何在Dll中获取表单列表 [英] How to get the List of forms in Dll
问题描述
HI Everyone,
我创建了一个类库(Test.dll)并添加了几个表单。
和创建的Windows窗体应用程序添加了对Test.dll的引用。
在窗体加载中为库创建对象并获取窗体。
Test.Form1 frm = new Test.Form1();
但我希望dll中存在所有表格。< br $>
请帮助我。
谢谢,
HI Everyone,
I have Created a class library(Test.dll) and added few forms.
and created windows forms application added reference to Test.dll.
In form load Created object for library and getting the form.
Test.Form1 frm=new Test.Form1();
But i want all the forms present in the dll.
Kindly help me anyone.
Thanks,
推荐答案
假设您要列出程序集中的所有表单类,这并不太难。
Assuming that you want to list all the form classes in the assembly, it's not too difficult.
string assemblyPath = @"D:\Temp\Test.DLL";
Assembly assembly = Assembly.LoadFrom(assemblyPath);
foreach (Type type in assembly.GetTypes())
{
if (type.BaseType == typeof(Form))
{
Console.WriteLine(type.FullName);
}
}
您还可以使用反射创建每个的实例,这也非常简单:
You can also use reflection to create an instance of each that's also pretty simple:
string assemblyPath = @"D:\Temp\Test.DLL";
List<Form> forms = new List<Form>();
Assembly assembly = Assembly.LoadFrom(assemblyPath);
foreach (Type type in assembly.GetTypes())
{
if (type.BaseType == typeof(Form))
{
forms.Add((Form) Activator.CreateInstance(type));
}
}
但如果您知道程序集中表单的名称,那么使用它会容易得多!
But it's a lot easier to work with if you know the names of the forms in the assembly!
这篇关于如何在Dll中获取表单列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!