抽象工厂模式 中遇到 Type type = Type.GetType(name, true);//在这部会抛出异常,这怎么解决呢?-->求解
class Program { static void Main(string[] args) { string fruitName = Console.ReadLine(); IFruit myFruit; myFruit = FruitFactory.MakeFruit(fruitName); Console.ReadKey(); } } public interface IFruit { } public class Orange : IFruit { public Orange() { Console.WriteLine("Orange is over"); } } public class Apple : IFruit { public Apple() { Console.WriteLine("Apple is over"); } } public static class FruitFactory { public static IFruit MakeFruit(string name) { /* switch (name) { case "Orange": return new Orange(); case "Apple": return new Apple(); default: return null; } */ // /* IFruit MyFruit = null; try { Type type = Type.GetType(name, true);//在步会出现以下异常,这怎么解决呢? //Could not load type 'Apple' from assembly 'CS Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. MyFruit = (IFruit)Activator.CreateInstance(type);//Activator 包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用。无法继承此类。 } catch (TypeLoadException ex) { Console.WriteLine("我去,还能这样用。 exception caught:{0}", ex.Message); } return MyFruit; //*/ } }