从网上下了段代码,试运行报错:
“未能从程序集“FruitFactory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中加载类型“Apple”。”以下是源代码:(是个C#控制台程序)using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace FruitFactory
{
    class Program
    {
        
        //接口
        public interface IFruit
        {
            
        }        //橘子
        public class Orange : IFruit
        {
            public Orange()
            {
                Console.WriteLine("An orange is got!");
            }
        }        //苹果
        public class Apple : IFruit
        {
            public Apple()
            {
                Console.WriteLine("An apple is got!");
            }
        }        //工厂类
        public class FruitFactory
        {
            private static FruitFactory _instance = new FruitFactory();
            public static FruitFactory Instance
            {
                get { return _instance; }
            }            //工厂方法
            public IFruit MakeFruit(string Name)
            {
                IFruit MyFruit = null;
                try
                {
                    
                    //此处报错
                    Type type = Type.GetType(Name,true);
                    MyFruit = (IFruit)Activator.CreateInstance(type);                }
                catch (TypeLoadException e)
                {
                    Console.WriteLine("I dont know this kind of fruit,exception caught - {0}", e.Message);
                }
                //返回创建的对象
                return MyFruit;
            }
        }        static void Main(string[] args)
        {
            string FruitName = Console.ReadLine();
            FruitFactory.Instance.MakeFruit(FruitName);         }
    }
}

解决方案 »

  1.   


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
    namespace ConsoleTest
    {
        using System;
        using System.Collections.Generic;
        using System.Text;
        class Program
        {
            static void Main(string[] args)
            {
                string FruitName = Console.ReadLine();
                FruitFactory.Instance.MakeFruit(FruitName);
            }
        }
            //接口 
            public interface IFruit
            {
            }
            //橘子 
            public class Orange : IFruit
            {
                public Orange()
                {
                    Console.WriteLine("An orange is got!");
                }
            }        //苹果 
            public class Apple : IFruit
            {
                public Apple()
                {
                    Console.WriteLine("An apple is got!");
                   
                }
              
            }        //工厂类 
            public class FruitFactory
            {
                private static FruitFactory _instance = new FruitFactory();
                public static FruitFactory Instance
                {
                    get { return _instance; }
                }            //工厂方法 
                public IFruit MakeFruit(string Name)
                {
                    IFruit MyFruit = null;
                    try
                    {                    //此处报错 
                        //Type type = Type.GetType(Name, true);
                        MyFruit = (IFruit)Assembly.Load("ConsoleTest").CreateInstance("ConsoleTest.Orange");                }
                    catch (TypeLoadException e)
                    {
                        Console.WriteLine("I dont know this kind of fruit,exception caught - {0}", e.Message);
                    }
                    //返回创建的对象 
                    return MyFruit;
                }
            }
    }
      

  2.   


    谢谢!这样就不报错了!
    不过控制台怎么一闪就没了?怎么能显示“An apple is got!”
    谢谢了!
      

  3.   

    控制台一闪就没了?加上句 Console.ReadKey();
      

  4.   

    或者直接按ctrl+f5 (开始执行不挑食)
      

  5.   

    还是在输入“Apple”后控制台没有显示"An apple is got!",哪里写错了么?
      

  6.   

     //此处报错 
                        Type type = Type.GetType(Name,true); 
                        MyFruit = (IFruit)Activator.CreateInstance(type); 
    ----------------------------------------------------------------------------
     //此处报错                     MyFruit = (IFruit)Assembly.Load("ConsoleTest").CreateInstance("ConsoleTest.Orange");请问这两者有什么不同么?为什么上面的就不行呢?
      

  7.   

    居然这么用工厂模式
    给你一个经典的
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Configuration;
    namespace configuration
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Factory factory=new Factory();
                factory.FDoWork(new FXY());
                
            }
        }    interface IMyInterface
        {
            void Work();
        }
        class GR : IMyInterface
        {
            void IMyInterface.Work()
            {
                MessageBox.Show("工人做工");
            }    }
        class NM : IMyInterface
        {
            void IMyInterface.Work()
            {
                MessageBox.Show("农民做工");
            }
        }
        class FXY : IMyInterface
        {
            void IMyInterface.Work()
            {
                MessageBox.Show("飞行员开飞机");
            }
        }
        class Factory
        {
            private IMyInterface _myInterface;
            public IMyInterface MyInterface
            {
                get { return _myInterface; }
            }
            public void  FDoWork(object c)
            {
                try
                {                _myInterface =(IMyInterface) c;            }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (_myInterface != null)
                        _myInterface.Work();
                }
            }        }
            
            
        }