[Mono-list] Activator.CreateInstance issue

Tom Cabanski tcabanski@OAI.cc
Sat, 8 Mar 2003 16:07:56 -0600


Currently, attempting to pass a null parameter to a constructor using
Activator.CreateInstance fails with "A null value was found where an
object instance was required".  Under Microsoft.NET, this passes a null
value to the constructor as expected. =20

I've looked at the source and can see where the problem is; the code
needs the attributes types to go look up the constructor.  Obviously,
Microsoft is using some other method when it encounters null parameters
(perhaps they go look up the closest match if they have one or more null
parameters). =20

Should I just add a bug at this point?  My current sample is a very
small console application.  Would a Nunit test be better?  Are you using
Nunit 1 or 2?

Is there a FAQ or instructions regarding setting up a development
environment?  I would like to make a quick patch here so I can move
forward with my porting test. I'm not at all certain that what I come up
with will be very efficient but I can certainly pass along the solution
once I have it.

Here's the example:

using System;
using System.Reflection;

namespace TestReflection
{
    class Foo
    {
        private string _name;
       =20
        public Foo(string name)
        {
            if (name =3D=3D null)
            {
                _name =3D "NULL passed";
            }=20
            else=20
            {
                _name =3D name;
            }
        }

        public override string ToString()
        {
            return _name;
        }
    }
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            Type t =3D
Assembly.GetExecutingAssembly().GetType("TestReflection.Foo");
            if (t =3D=3D null)
            {
                Console.WriteLine("No type");
            }=20
            else=20
            {
                //this one works under Mono
                object [] o =3D new object[1];
                string name =3D "Value Passed";
                o[0] =3D name;
                try
                {
                    Foo foo =3D (Foo)Activator.CreateInstance(t, o);
                    Console.WriteLine(foo.ToString());
                }=20
                catch (Exception err)
                {
                    Console.WriteLine("Unexpected failure under Mono
{0}", err.Message);
                }

                //this one fails under Mono (works on .NET)
                o[0] =3D null;
                try
                {
                    Foo foo =3D (Foo)Activator.CreateInstance(t, o);
                    Console.WriteLine(foo.ToString());
                }=20
                catch (Exception err)
                {
                    Console.WriteLine("Expected failure under Mono {0}",
err.Message);
                }
            }
        }
    }
}

-----------------------------
TFC