C#从项目B获取项目A的表单名称 [英] C# Get form names of project A from Project B

查看:80
本文介绍了C#从项目B获取项目A的表单名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个解决方案中有两个项目,分别是project Aproject B(using VS2010 Ultimate and C# windows application). Project B充当project A的用户管理应用程序. 在project B中,我有一个包含chekcedlistbox控件的表单,该控件将列出所有project A表单的名称和文本(此表单将使系统管理员可以根据用户的安全组向他们授予允许查看/编辑的表单. ) 这是我的代码:

I have two Projects in one solution, project A and project B (using VS2010 Ultimate and C# windows application). Project B acts as a user management application for project A. In project B i have a form that contains a chekcedlistbox control that will list all project A forms names and texts (this form will let system administrator to grant users the forms that are allowed to view/edit based on their security groups) this is my code:

   private void GetFormNames()
    {
        foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
        {
            foreach (Type t in a.GetTypes())
            {
                if (t.BaseType == typeof(Form))
                {
                    var emptyCtor = t.GetConstructor(Type.EmptyTypes);
                    if (emptyCtor != null)
                    {
                        var f = (Form)emptyCtor.Invoke(new object[] { });
                        string FormText = f.Text;
                        string FormName = f.Name;
                        checkedListBox1.Items.Add("" + FormText + "//" + FormName + "");
                    }
                }
            }

        }

    }

我得到的结果是当前项目(B),空行(//)和Select Window//MdiWindowDialogPrintPreview的表单名称.

the result i am getting is the form names of my current project (B) and Empty lines(//) and Select Window//MdiWindowDialog, PrintPreview.

推荐答案

我将假定您正确引用了ProjectA,并且您感兴趣的所有表单实际上都具有public无参数构造函数.该问题很可能是由于尚未加载ProjectA引起的,您可以通过多种方式解决此问题.也许最直接的方法是使用 static Assembly.Load (只要这些文件位于同一目录中,否则文件将变得更加复杂.

I'm going to assume you've referenced ProjectA correctly and all the forms you're interested in actually have a public parameterless constructor. The problem is likely caused by ProjectA not being loaded yet, you can fix this in multiple ways. Probably the most direct is to use the static Assembly.Load (as long as the files are in the same directory, if not it gets more complicated).

try
{
    Assembly projectA = Assembly.Load("ProjectA"); // replace with actual ProjectA name 
    // despite all Microsoft's dire warnings about loading from a simple name,
    // you should be fine here as long as you don't have multiple versions of ProjectA
    // floating around

    foreach (Type t in projectA.GetTypes())
    {
        if (t.BaseType == typeof(Form))
        {
            var emptyCtor = t.GetConstructor(Type.EmptyTypes);
            if (emptyCtor != null)
            {
                var f = (Form)emptyCtor.Invoke(new object[] { });
                // t.FullName will help distinguish the unwanted entries and
                // possibly later ignore them
                string formItem = t.FullName + " // " + f.Text + " // " + f.Name;
                checkedListBox1.Items.Add(formItem);
            }
        }
    }
}
catch(Exception err)
{
    // log exception
}

另一个(可能更干净的解决方案)是让您感兴趣的所有表单都从一个基本表单继承.然后,您可以从已知的Type中加载程序集,并在将每个枚举的Type添加到列表之前,检查每个枚举的Type都继承自该程序集.但是,这是更广泛的更改,涉及到ProjectA.

Another (probably cleaner solution) would be to have all the forms you're interested in inherit from a single base form. You could then load the assembly from that known Type and check that each enumerated Type inherits from it before adding it to your list. This is a more extensive change, however, and touches ProjectA.

这篇关于C#从项目B获取项目A的表单名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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