简体   繁体   English

从我的项目中检索所有表格

[英]retrieving all forms from my project

I know this question has been asked before. 我知道这个问题曾经被问过。 I have the code to do this but i am getting an error and I think I know why but I am just really getting into .Net Reflection so I wish to get confirmation on whether I am correct or not. 我有执行此操作的代码,但是我遇到错误,我想我知道为什么,但是我只是真的进入.Net Reflection,所以我希望获得有关我是否正确的确认。

Here is the code. 这是代码。 I want to retrieve all the forms from my project that have a basetype of "BaseEditForm" and then all of those that end with "EditForm" I want to put in a List to populate a ListBox. 我想从我的项目中检索所有具有“ BaseEditForm”基本类型的表单,然后将所有以“ EditForm”结尾的表单添加到列表中以填充ListBox。

public void LoadAllEditForms()
        {
            formsList = new List<string>();

            try
            {
                Assembly project = Assembly.Load("UnionAdministrator");

                foreach (Type t in project.GetTypes())
                {
                    if (t.BaseType == typeof (BaseEditForm))
                    {
                        var emptyCtor = t.GetConstructor(Type.EmptyTypes);
                        if (emptyCtor != null)
                        {
                            var f = (Form) emptyCtor.Invoke(new object[] {});
                            if (f.Name.EndsWith("EditForm"))
                                formsList.Add(f.Name);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

I am getting the error message "Object reference not set to an instance of an object." 我收到错误消息“对象引用未设置为对象的实例”。 on the line 在线上

var f = (Form) emptyCtor.Invoke(new object[] {});

emptyCtor is not null and besides there is no way for emptyCtor to get to this point if it is null. emptyCtor不为null,此外,如果emptyCtor为null,则没有办法达到这一点。 So I am confused about the error message. 所以我对错误消息感到困惑。 So here is my question. 所以这是我的问题。 For this to work properly do all my forms have to have a default constructor? 为了使其正常工作,我所有的表单都必须具有默认构造函数吗? Almost all my forms have a constructor that takes one or more parameters. 我几乎所有的表单都有一个带有一个或多个参数的构造函数。 Is that my problem? 那是我的问题吗?

Your code works fine. 您的代码工作正常。
It must be one of your constructors that throw the exception. 它必须是引发异常的构造函数之一。

Check all of your derived Forms to see if any of them (those that does not take any ctor parameters) could throw a NullReferenceException if invoked. 检查所有派生的Form,以查看它们中的任何一个(不带任何ctor参数的表单)在调用时是否可能引发NullReferenceException。

I'm not sure why you're going through the trouble of trying to execute the constructor of every form. 我不确定您为什么要尝试执行每种表单的构造函数的麻烦。 You could simplify your code (and avoid the whole issue) by just looking at the type names. 您只需查看类型名称即可简化代码(并避免整个问题)。

public void LoadAllEditForms()
{
    Assembly project = Assembly.Load("UnionAdministrator");

    var formsList = project.GetTypes()
        .Where (t => t.BaseType == typeof(BaseEditForm) && t.Name.EndsWith("EditForm"))
        .ToList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM