简体   繁体   English

创建表单变形实例Activator.CreateInstance的实例

[英]Create instance of form trought Activator.CreateInstance

Hello I would like to found solution of my problem,i found there is method from .net Activator.CreateInstance but I realy dont understand now why it didnt work form me.. 您好我想找到我的问题的解决方案,我发现有.net Activator.CreateInstance的方法,但我现在真的不明白为什么它对我不起作用。

public Form1()
{
    InitializeComponent();
}

private void ToolStripMenuItem_Click(object sender, EventArgs e)
{


    OpenFormInPanel(object name_of_form,Control panel....,);


}

private void OpenFormInPanel(object name_of_form,Control panel....,)
{
      var objForm = (Form)Activator.CreateInstance(Type.GetType("WindowsFormsApp12.Form2"), 1, "test");
        //   Form2 objForm = new Form2();
        // Form objForm = (Form)handle.Unwrap();
        //ObjectHandle objForm = Activator.CreateInstance("Namespace.Forms", "Form2");

        objForm.TopLevel = false;
        panel1.Controls.Add(objForm);
        objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        objForm.Dock = DockStyle.Fill;
        objForm.Show();
}

This is Form2 这是Form2

namespace WindowsFormsApp12
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
    }
}

Im thanks full for every help. 我非常感谢您的所有帮助。

Activator.CreateInstance is a way of creating a class instance using a Type . Activator.CreateInstance是一种使用Type创建类实例的方法。 If you know exactly which type you need to create then there's probably no reason to use it. 如果您确切地知道需要创建哪种类型,则可能没有理由使用它。

You could just do this: 您可以这样做:

var objForm = new Form2();

CreateInstance is really just doing the same thing - it's calling the constructor. CreateInstance实际上只是在做同样的事情-它在调用构造函数。

The overload you're calling is going to take the other arguments and try to pass them to the constructor. 您正在调用重载将采用其他参数,并尝试将其传递给构造函数。 So this: 所以这:

var objForm = (Form)Activator.CreateInstance(Type.GetType("WindowsFormsApp12.Form2"), 1, "test");

is basically the same as this: 基本上与此相同:

var objForm = new Form2(1, "test");

If you try typing that in your application you'll probably see a compile error because Form2 doesn't have a constructor that takes an integer and a string (1, "test") . 如果尝试在应用程序中键入该内容,则可能会看到编译错误,因为Form2没有采用整数和字符串(1, "test")的构造函数。 That's probably why it's failing. 这可能就是为什么它失败了。 It's trying to call a constructor that doesn't exist. 它试图调用一个不存在的构造函数。


While I was typing this detail was added in the comments: 在我键入时,此详细信息已添加到注释中:

System.MissingMethodException: The WindowsFormsApp12.Form2 constructor was not found System.MissingMethodException:找不到WindowsFormsApp12.Form2构造函数

If you go to the link to that method (it's linked above) it tells you the different exceptions that it might throw and why. 如果转到该方法的链接(在上面链接),它将告诉您它可能引发的不同异常以及原因。

It says: 它说:

MissingMethodException MissingMethodException

In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, MissingMemberException, instead. 在Windows Store应用程序的.NET或可移植类库中,捕获基类异常MissingMemberException。

No matching public constructor was found. 找不到匹配的公共构造函数。

So that's exactly it. 就是这样。 It's trying to call a constructor that takes an integer and a string but the class doesn't have one. 它试图调用一个接受一个整数和一个字符串但该类没有一个的构造函数。

Unless you definitely need to use Activator.CreateInstance I wouldn't try fixing the way you use it. 除非您确实需要使用Activator.CreateInstance否则我不会尝试修复使用方式。 I would just call the constructor the "normal" way. 我只是将构造函数称为“常规”方法。 Or maybe start with that, get it working, and then if you need CreateInstance change it back to that once you know which constructor you want to call. 或者也许从此开始,使其工作,然后,如果需要CreateInstance ,则在知道要调用的构造函数后将其更改回原来的状态。

That's a handy tool that a lot of developers don't use. 这是许多开发人员不使用的便捷工具。 If you're calling a method of class in the .NET Framework and it either throws an exception and you don't know why, or you want to know what exceptions it might throw, you can go to the documentation for that method and it will usually tell you what exceptions it throws and why. 如果要在.NET Framework中调用类的方法,并且它抛出异常并且不知道为什么,或者想知道它可能引发什么异常,则可以转到该方法及其相关文档通常会告诉您它引发什么异常以及原因。 You can usually just highlight the method in your code and press F1. 通常,您只需突出显示代码中的方法,然后按F1。

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

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