[Mono-list] Instantiating an object via AppDomain.CreateInstance

Yuri Leikind y.leikind@sam-solutions.net
Thu, 17 Jun 2004 18:55:14 +0300


Hello all,


If I instantiate an object via AppDomain.CreateInstance , and this object's
class has a constructor with parameters, I get the exception

	Unhandled Exception: System.MissingMethodException: Constructor not found.

though needed parameters are passed to AppDomain.CreateInstance

I found it out trying an example from Programming C#, 2nd edition. This example
runs on MS .NET and fails on Mono.

Here is it :

====================================
using System;
using System.Runtime.Remoting;
using System.Reflection;
namespace ProgCSharp
{
   // for marshal by reference comment out
   // the attribute and uncomment the base class
   [Serializable]
   public class Point // : MarshalByRefObject
   {
      public Point (int x, int y)
      {
         Console.WriteLine( "[{0}] {1}",
            System.AppDomain.CurrentDomain.FriendlyName,
            "Point constructor");
         this.x = x;
         this.y = y;
      }
      public int X
      {
         get
         {
            Console.WriteLine( "[{0}] {1}",
               System.AppDomain.CurrentDomain.FriendlyName,
               "Point x.get");
            return this.x;
         }
         set
         {
            Console.WriteLine( "[{0}] {1}",
               System.AppDomain.CurrentDomain.FriendlyName,
               "Point x.set");
            this.x = value;
         }
      }
      public int Y
      {
         get
         {
            Console.WriteLine( "[{0}] {1}",
               System.AppDomain.CurrentDomain.FriendlyName,
               "Point y.get");
            return this.y;
         }
         set
         {
            Console.WriteLine( "[{0}] {1}",
               System.AppDomain.CurrentDomain.FriendlyName,
               "Point y.set");
            this.y = value;
         }
      }


   private int x;
   private int y;
}
// the shape class marshals by reference
public class Shape : MarshalByRefObject
{
   public Shape(int upperLeftX, int upperLeftY)
   {
      Console.WriteLine( "[{0}] {1}",
         System.AppDomain.CurrentDomain.FriendlyName,
         "Shape constructor");
      upperLeft = new Point(upperLeftX, upperLeftY);
   }
   public Point GetUpperLeft( )
   {
      return upperLeft;
   }
   public void ShowUpperLeft( )
   {
      Console.WriteLine( "[{0}] Upper left: {1},{2}",
         System.AppDomain.CurrentDomain.FriendlyName,
         upperLeft.X, upperLeft.Y);
   }
   private Point upperLeft;
}
public class Tester
{
   public static void Main( )
   {
      Console.WriteLine( "[{0}] {1}",
         System.AppDomain.CurrentDomain.FriendlyName,
         "Entered Main");
      // create the new app domain
      AppDomain ad2 =
         AppDomain.CreateDomain("Shape Domain");
      // Assembly a = Assembly.LoadFrom("ProgCSharp.exe");
      // Object theShape = a.CreateInstance("Shape");
      // instantiate a Shape object
      ObjectHandle oh = ad2.CreateInstance(
         "ProgCSharp",
         "ProgCSharp.Shape", false,
         System.Reflection.BindingFlags.CreateInstance,
         null, new object[] {3, 5},
         null, null, null );
      Shape s1 = (Shape) oh.Unwrap( );
      s1.ShowUpperLeft( );     // ask the object to display
      // get a local copy? proxy?
      Point localPoint = s1.GetUpperLeft( );
        // assign new values
        localPoint.X = 500;
        localPoint.Y = 600;
        // display the value of the local Point object
        Console.WriteLine( "[{0}] localPoint: {1}, {2}",
           System.AppDomain.CurrentDomain.FriendlyName,
           localPoint.X, localPoint.Y);
        s1.ShowUpperLeft( );     // show the value once more
      }
   }
}

====================================

Here is the output:

leikind@pc324:~$ mono ProgCSharp.exe 
[ProgCSharp.exe] Entered Main

Unhandled Exception: System.MissingMethodException: Constructor not found.

Server stack trace: 
in <0x0019a> System.Activator:CreateInstance (System.Type,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo,object[])
in <0x00074> System.Activator:CreateInstance (string,string,bool,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo,object[],System.Security.Policy.Evidence)
in <0x00055> System.AppDomain:CreateInstance (string,string,bool,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo,object[],System.Security.Policy.Evidence)
in (unmanaged) (wrapper managed-to-native) System.Runtime.Remoting.RemotingServices:InternalExecute (System.Reflection.MonoMethod,object,object[],object[]&)
in <0x00004> (wrapper managed-to-native) System.Runtime.Remoting.RemotingServices:InternalExecute (System.Reflection.MonoMethod,object,object[],object[]&)
in <0x00172> System.Runtime.Remoting.RemotingServices:InternalExecuteMessage (System.MarshalByRefObject,System.Runtime.Remoting.Messaging.IMethodCallMessage)


Exception rethrown at [0]: 

in <0x00748> System.Runtime.Remoting.Proxies.RealProxy:PrivateInvoke (System.Runtime.Remoting.Proxies.RealProxy,System.Runtime.Remoting.Messaging.IMessage,System.Exception&,object[]&)

=======================================

Does anyone have a clue to this incompatibility?  

-- 
Best regards,
Yuri Leikind