简体   繁体   中英

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..

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

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 . 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.

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") . 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

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

In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, MissingMemberException, instead.

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. 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.

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. You can usually just highlight the method in your code and press F1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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