Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Urgent -- Strange MissingMethodException -- Activator.CreateInstance

Posted on 2004-09-21
10
Medium Priority
?
1,366 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

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!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

877 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