开发者

should Assembly.GetExecutingAssembly().CreateInstance throw an exception here?

I'm pretty new, so this may be a "stupid question," or may not be appropriate here, please advise as appropriate.

I'm exploring some of C#'s features, this week I'm messing about with reflection. I'm confused when I read http://msdn.microsoft.com/en-us/library/145sfyea.aspx, from what I can tell I'm not getting MissingMethodException (No matching constructor was found.) when I think I should.

Question: should this code throw an exception at the noted point?

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Test
{
    abstract class Vehicle
    {
        public string type { get; set; }
    }

    class Van : Vehicle
    {
        public Van()
        {
            System.Console.WriteLine("van!");
            this.type = "van";
            // ...
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            List<String> things = new List<String>();
            things.Add("Van");
         开发者_高级运维   things.Add("Car");

            List<Vehicle> inventory = new List<Vehicle>();

            foreach (String s in things)
            {
                Vehicle vehicle = Assembly.GetExecutingAssembly().CreateInstance("Test" + "." + s, true) as Vehicle;
                if (vehicle != null)
                {
                    inventory.Add(vehicle);
                }
                else
                {
                    System.Console.WriteLine("Should an attempt to create an instance of"+ "Test" + "." + s+ " have thrown an exception? " );
                };
            }
            Console.Read();
            Console.Read();
        }

    }

}


No. You would get a MissingMethodException if the Test.Car type existed, but did not have a public parameterless constructor. If the type cannot be found at all, null is returned; if the type is found, but a public constructor matching the argument list you have provided cannot be located, then a MissingMethodException is thrown.

From MSDN:

Return Value
Type: System.Object
An instance of the specified type created with the default constructor; or null if typeName is not found.

See this ideone example.


It would, but only if it could not find the constructor in an existing type. (ie. it is private). In your code, there is no 'Car' class, so there is no type for it to search for a constructor on, and thus returns a null object.

This would throw a MissingMethodException:

class Car : Vehicle {
    private Car() { // I'm private and you cannot find me.
        System.Console.WriteLine("car!");
        this.type = "car";
    }
}


If the type's constructor cannot be resolved then sure, I'd expect an exception. But the method is documented as returning null if the type in question cannot be found.

There are methods like Type.GetType() that accept a boolean flag which determines if an exception should be raised... I suppose this particular method would benefit from that at some level, but you can always just call Type.GetType() before you try to create an instance, and specify you want an exception if the type cannot be located.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜