Solved

Urgent -- Strange MissingMethodException -- Activator.CreateInstance

Posted on 2004-09-21
10
1,222 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
  • 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now