using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using ClassLibrary1;
namespace ConsoleApplication1
{
public class FruitFactory
{
public IFruit MakeFruit(string Name)
{
IFruit MyFruit = null;
try
{
Assembly assembly = Assembly.Load("ClassLibrary1");
Type type = assembly.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;
}
}
class Program
{
static void Main(string[] args)
{
string FruitName = Console.ReadLine();
IFruit MyFruit = null;
FruitFactory MyFruitFactory = new FruitFactory();
MyFruit = MyFruitFactory.MakeFruit(FruitName);
}
}
}
----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
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!");
}
}
}
红的怎么不行啊