Urgent -- Strange MissingMethodException -- Activator.CreateInstance

HI Experts,

I have one baseclass derived from System.Windows.Forms.Form ---- suppose BaseOne
then i have another derivedClass derived from BaseOne --- suppose DerivedOne

Now, in a class i am passing the typeof(DerivedOne) and in that class,

through Activator.CreateInstance i am creating object of that derived class (which will default look for zero parameter constructor) --- and then explicitly convert it to the BaseOne type.

Like this,
BaseOne myForm = (BaseOne)Activator.CreateInstance(typeof(DerivedOne));

All this works fine with my test project. and it is creating the object and converting to the BaseOne. But when i do the same thing with my Main project, it is not able to find the Constructor to create the Object and convert it to the BaseOne method.

The constructor is present there, still it is not able to find the zero parameter constructor to create object of the DerivedOne.
and is giving me "MissingMethodException" error.......

This is so strange that i can not tell you, because the same same same thing with the test project is working and not with the main project and it is not able to find the constructor to create object.

This is really some weird thing..........can anybody help me to solve this problem ??

Thanks in Advance.
Chintan.
LVL 3
chintan_vaishyaAsked:
Who is Participating?
 
God_AresConnect With a Mentor Commented:
no problems with problem,.. here check out my app (Yes cf):

I think you are Pinvoking,... could that be right?

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace SmartDeviceApplication1
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Button button2;
            private System.Windows.Forms.MainMenu mainMenu1;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  base.Dispose( disposing );
            }
            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.mainMenu1 = new System.Windows.Forms.MainMenu();
                  this.button1 = new System.Windows.Forms.Button();
                  this.button2 = new System.Windows.Forms.Button();
                  //
                  // button1
                  //
                  this.button1.Location = new System.Drawing.Point(24, 40);
                  this.button1.Size = new System.Drawing.Size(184, 168);
                  this.button1.Text = "button1";
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  //
                  // button2
                  //
                  this.button2.Location = new System.Drawing.Point(168, 232);
                  this.button2.Size = new System.Drawing.Size(56, 24);
                  this.button2.Text = "button2";
                  this.button2.Click += new System.EventHandler(this.button2_Click);
                  //
                  // Form1
                  //
                  this.Controls.Add(this.button2);
                  this.Controls.Add(this.button1);
                  this.Menu = this.mainMenu1;
                  this.Text = "Form1";

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>

            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void button1_Click(object sender, System.EventArgs e)
            {
                  
                  DerivedOne d  = (DerivedOne)Activator.CreateInstance(typeof(DerivedOne));
                  d.ShowDialog();
            
            }

            private void button2_Click(object sender, System.EventArgs e)
            {
                  Close();
            }
      }

      public class DerivedOne : Form1
      {
            public DerivedOne():base()
            {
                  Text = "Derived one";
            }
      }
}
0
 
armoghanCommented:
This is strange
try different contructors of Activator.CreateInstance
0
 
armoghanCommented:
try using Type.GetType() instead of typeof()
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
chintan_vaishyaAuthor Commented:
Hi armoghan,

this is a Pocket PC application in .NET Compact Framework. and Compact framework is only supporting

Activator.CreateInstance(typeof())

nothing else.

0
 
chintan_vaishyaAuthor Commented:
does not work........

I m just getting mad........because it works with test project but not with my main project where it need to be worked.......

0
 
NipNFriar_TuckCommented:
You have probably already checked these but just to be sure...

Do you have an over loaded constructor?  If so have you explicitly defined the empty constructor?
Is you're constructor public?

Have you tried removing/commenting out all code from your constructor, to define if it is the constructor or reflection?

Is the test project and main project on the same computer?



Some of my wierdest issues have been from something simple that I over looked.
0
 
chintan_vaishyaAuthor Commented:
HI,

i have checked all those things. Both projects are on same computer.
But i did not get "Have you tried removing/commenting out all code from your constructor, to define if it is the constructor or reflection?"

What do u mean by that?? what should i check by removing the code from constructor ??

Thanks for ur help.
Chintan.
0
 
NipNFriar_TuckCommented:
If you remove or comment out  the code in the constructor then run your project to see what error you get, if it is the same then it is most likely something being done in reflection, otherwise it is one of the lines in the constructor...  Armed with this information you can then start adding blocks back into the constructor until you come up with the problem line, at which time you will have found the hidden problem line...
0
 
armoghanCommented:
can you do
t = typeof(DerivedOne)
and then see all the construtors info like
ConstructorInfo[] ctrInfo=t.GetConstructors();
0
 
God_AresCommented:
I think you should just make your class like

Derivedone d = new Derivedone();

why are you using the Activator? Plugin? if so make a reference to your dll and create it the normal way.

perhaps you should post some code,..

regards

Floris Briolas
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.