zoukankan      html  css  js  c++  java
  • 抽象工厂模式 中遇到 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;
                //*/
            }
        }
  • 相关阅读:
    Tomcat
    二叉树
    CDOJ 1962 天才钱vs学霸周2【最大流】
    次小生成树(POJ1679/CDOJ1959)
    CDOJ1927 爱吃瓜的伊卡洛斯(2) 【并查集】启发式合并+set
    HDU 1074 Doing Homework(DP状态压缩)
    uva 11367 (Dijkstra+DP)
    线段树模板
    openpose pytorch代码分析
    opencv图片坐标和数组下标
  • 原文地址:https://www.cnblogs.com/potoofly/p/2958529.html
Copyright © 2011-2022 走看看