Solved

Urgent -- Strange MissingMethodException -- Activator.CreateInstance

Posted on 2004-09-21
10
1,306 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:chintan_vaishya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 18

Expert Comment

by:armoghan
ID: 12111361
This is strange
try different contructors of Activator.CreateInstance
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12111391
try using Type.GetType() instead of typeof()
0
 
LVL 3

Author Comment

by:chintan_vaishya
ID: 12111396
Hi armoghan,

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

Activator.CreateInstance(typeof())

nothing else.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:chintan_vaishya
ID: 12111511
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
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12112628
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
 
LVL 3

Author Comment

by:chintan_vaishya
ID: 12112722
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
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12112785
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
 
LVL 18

Expert Comment

by:armoghan
ID: 12119571
can you do
t = typeof(DerivedOne)
and then see all the construtors info like
ConstructorInfo[] ctrInfo=t.GetConstructors();
0
 
LVL 7

Accepted Solution

by:
God_Ares earned 500 total points
ID: 12123295
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
 
LVL 7

Expert Comment

by:God_Ares
ID: 12123325
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question