定义简单的反射工厂示例
首先,定义一个水果抽象类,代码如下:
class Fruit
{
//定义虚方法
public virtual void Eating()
{
Console.WriteLine("水果有各种吃法。。。");
}
}
{
//定义虚方法
public virtual void Eating()
{
Console.WriteLine("水果有各种吃法。。。");
}
}
然后,实例化几个水果类,代码如下:
class Banana : Fruit
{
public override void Eating()
{
Console.WriteLine("香蕉扒皮吃。。。");
}
}
class Orange : Fruit
{
public override void Eating()
{
Console.WriteLine("橘子剥皮吃。。。");
}
}
class Apple : Fruit
{
public new void Eating()
{
Console.WriteLine("苹果洗了吃。。。");
}
//public override void Eating()
//{
// Console.WriteLine("苹果洗了吃。。。");
//}
}
{
public override void Eating()
{
Console.WriteLine("香蕉扒皮吃。。。");
}
}
class Orange : Fruit
{
public override void Eating()
{
Console.WriteLine("橘子剥皮吃。。。");
}
}
class Apple : Fruit
{
public new void Eating()
{
Console.WriteLine("苹果洗了吃。。。");
}
//public override void Eating()
//{
// Console.WriteLine("苹果洗了吃。。。");
//}
}
最后,创建水果工厂,代码如下:
//水果工厂
class FruitFactory
{
//生成水果
public Fruit CreateFruit(string _fruitname)
{
//不使用反射的做法如下:
//if ("Apple" == _fruitname)
//{
// return new Apple();
//}
//else if ("Banana" == _fruitname)
//{
// return new Banana();
//}
//else if ("Orange" == _fruitname)
//{
// return new Orange();
//}
//else
//{
// throw new Exception("您指定的水果不生产!");
//}
//获得当前程序的命名空间
string strNamespace = Assembly.GetExecutingAssembly().GetName().Name;
//调用方法一:使用 Type 类
//Type type = Type.GetType("ConsoleApplication1." + _fruitname);
//ConstructorInfo ctorInfo = type.GetConstructor(System.Type.EmptyTypes);
//// Invoke()方法:返回与构造函数关联的类的实例。
//Fruit fruitA = (Fruit)ctorInfo.Invoke(new object[0]);
//return fruitA;
//调用方法二:使用 Assembly 类
//Assembly myAssembly = Assembly.GetExecutingAssembly();
//Fruit fruitB = (Fruit)myAssembly.CreateInstance("ConsoleApplication1." + _fruitname);
//return fruitB;
//调用方法三:使用 Activator 类
Fruit fruitC = (Fruit)Activator.CreateInstance(Type.GetType("ConsoleApplication1." + _fruitname, false, true));
return fruitC;
}
}
class FruitFactory
{
//生成水果
public Fruit CreateFruit(string _fruitname)
{
//不使用反射的做法如下:
//if ("Apple" == _fruitname)
//{
// return new Apple();
//}
//else if ("Banana" == _fruitname)
//{
// return new Banana();
//}
//else if ("Orange" == _fruitname)
//{
// return new Orange();
//}
//else
//{
// throw new Exception("您指定的水果不生产!");
//}
//获得当前程序的命名空间
string strNamespace = Assembly.GetExecutingAssembly().GetName().Name;
//调用方法一:使用 Type 类
//Type type = Type.GetType("ConsoleApplication1." + _fruitname);
//ConstructorInfo ctorInfo = type.GetConstructor(System.Type.EmptyTypes);
//// Invoke()方法:返回与构造函数关联的类的实例。
//Fruit fruitA = (Fruit)ctorInfo.Invoke(new object[0]);
//return fruitA;
//调用方法二:使用 Assembly 类
//Assembly myAssembly = Assembly.GetExecutingAssembly();
//Fruit fruitB = (Fruit)myAssembly.CreateInstance("ConsoleApplication1." + _fruitname);
//return fruitB;
//调用方法三:使用 Activator 类
Fruit fruitC = (Fruit)Activator.CreateInstance(Type.GetType("ConsoleApplication1." + _fruitname, false, true));
return fruitC;
}
}
测试代码如下:
class Program
{
static void Main(string[] args)
{
FruitFactory ff = new FruitFactory();
//打印(来自父类的):水果有各种吃法。。。
Fruit fA = ff.CreateFruit("Apple");
fA.Eating();
//打印(来自子类的):苹果洗了吃。。。
Apple apple = ff.CreateFruit("Apple") as Apple;
apple.Eating();
Fruit fB = ff.CreateFruit("Banana");
fB.Eating();
Fruit fC = ff.CreateFruit("Orange");
fC.Eating();
}
}
{
static void Main(string[] args)
{
FruitFactory ff = new FruitFactory();
//打印(来自父类的):水果有各种吃法。。。
Fruit fA = ff.CreateFruit("Apple");
fA.Eating();
//打印(来自子类的):苹果洗了吃。。。
Apple apple = ff.CreateFruit("Apple") as Apple;
apple.Eating();
Fruit fB = ff.CreateFruit("Banana");
fB.Eating();
Fruit fC = ff.CreateFruit("Orange");
fC.Eating();
}
}
利用反射创建实例对象的常用三种方式:
// 方式一:使用 Type 类
Type type = Type.GetType("ConsoleApplication1." + _fruitname);
ConstructorInfo ctorInfo = type.GetConstructor(System.Type.EmptyTypes);
// Invoke()方法:返回与构造函数关联的类的实例。
Fruit fruitA = (Fruit)ctorInfo.Invoke(new object[0]);
return fruitA;
ConstructorInfo ctorInfo = type.GetConstructor(System.Type.EmptyTypes);
// Invoke()方法:返回与构造函数关联的类的实例。
Fruit fruitA = (Fruit)ctorInfo.Invoke(new object[0]);
return fruitA;
// 方式二:使用 Assembly 类
Assembly myAssembly = Assembly.GetExecutingAssembly();
Fruit fruitB = (Fruit)myAssembly.CreateInstance("ConsoleApplication1." + _fruitname);
return fruitB;
Fruit fruitB = (Fruit)myAssembly.CreateInstance("ConsoleApplication1." + _fruitname);
return fruitB;
// 方式三:使用 Activator 类
Fruit fruitC = (Fruit)Activator.CreateInstance(Type.GetType("ConsoleApplication1." + _fruitname, false, true));
return fruitC;
return fruitC;
示例的全部代码如下:
View Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
//抽象类可以继承抽象类
namespace ConsoleApplication1
{
class Fruit
{
//定义虚方法
public virtual void Eating()
{
Console.WriteLine("水果有各种吃法。。。");
}
}
//水果工厂
class FruitFactory
{
//生成水果
public Fruit CreateFruit(string _fruitname)
{
//不使用反射的做法如下:
//if ("Apple" == _fruitname)
//{
// return new Apple();
//}
//else if ("Banana" == _fruitname)
//{
// return new Banana();
//}
//else if ("Orange" == _fruitname)
//{
// return new Orange();
//}
//else
//{
// throw new Exception("您指定的水果不生产!");
//}
//获得当前程序的命名空间
string strNamespace = Assembly.GetExecutingAssembly().GetName().Name;
//调用方法一:使用 Type 类
//Type type = Type.GetType("ConsoleApplication1." + _fruitname);
//ConstructorInfo ctorInfo = type.GetConstructor(System.Type.EmptyTypes);
//// Invoke()方法:返回与构造函数关联的类的实例。
//Fruit fruitA = (Fruit)ctorInfo.Invoke(new object[0]);
//return fruitA;
//调用方法二:使用 Assembly 类
//Assembly myAssembly = Assembly.GetExecutingAssembly();
//Fruit fruitB = (Fruit)myAssembly.CreateInstance("ConsoleApplication1." + _fruitname);
//return fruitB;
//调用方法三:使用 Activator 类
Fruit fruitC = (Fruit)Activator.CreateInstance(Type.GetType("ConsoleApplication1." + _fruitname, false, true));
return fruitC;
}
}
class Banana : Fruit
{
public override void Eating()
{
Console.WriteLine("香蕉扒皮吃。。。");
}
}
class Orange : Fruit
{
public override void Eating()
{
Console.WriteLine("橘子剥皮吃。。。");
}
}
class Apple : Fruit
{
public new void Eating()
{
Console.WriteLine("苹果洗了吃。。。");
}
//public override void Eating()
//{
// Console.WriteLine("苹果洗了吃。。。");
//}
}
class Program
{
static void Main(string[] args)
{
FruitFactory ff = new FruitFactory();
//打印(来自父类的):水果有各种吃法。。。
Fruit fA = ff.CreateFruit("Apple");
fA.Eating();
//打印(来自子类的):苹果洗了吃。。。
Apple apple = ff.CreateFruit("Apple") as Apple;
apple.Eating();
Fruit fB = ff.CreateFruit("Banana");
fB.Eating();
Fruit fC = ff.CreateFruit("Orange");
fC.Eating();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Reflection;
//抽象类可以继承抽象类
namespace ConsoleApplication1
{
class Fruit
{
//定义虚方法
public virtual void Eating()
{
Console.WriteLine("水果有各种吃法。。。");
}
}
//水果工厂
class FruitFactory
{
//生成水果
public Fruit CreateFruit(string _fruitname)
{
//不使用反射的做法如下:
//if ("Apple" == _fruitname)
//{
// return new Apple();
//}
//else if ("Banana" == _fruitname)
//{
// return new Banana();
//}
//else if ("Orange" == _fruitname)
//{
// return new Orange();
//}
//else
//{
// throw new Exception("您指定的水果不生产!");
//}
//获得当前程序的命名空间
string strNamespace = Assembly.GetExecutingAssembly().GetName().Name;
//调用方法一:使用 Type 类
//Type type = Type.GetType("ConsoleApplication1." + _fruitname);
//ConstructorInfo ctorInfo = type.GetConstructor(System.Type.EmptyTypes);
//// Invoke()方法:返回与构造函数关联的类的实例。
//Fruit fruitA = (Fruit)ctorInfo.Invoke(new object[0]);
//return fruitA;
//调用方法二:使用 Assembly 类
//Assembly myAssembly = Assembly.GetExecutingAssembly();
//Fruit fruitB = (Fruit)myAssembly.CreateInstance("ConsoleApplication1." + _fruitname);
//return fruitB;
//调用方法三:使用 Activator 类
Fruit fruitC = (Fruit)Activator.CreateInstance(Type.GetType("ConsoleApplication1." + _fruitname, false, true));
return fruitC;
}
}
class Banana : Fruit
{
public override void Eating()
{
Console.WriteLine("香蕉扒皮吃。。。");
}
}
class Orange : Fruit
{
public override void Eating()
{
Console.WriteLine("橘子剥皮吃。。。");
}
}
class Apple : Fruit
{
public new void Eating()
{
Console.WriteLine("苹果洗了吃。。。");
}
//public override void Eating()
//{
// Console.WriteLine("苹果洗了吃。。。");
//}
}
class Program
{
static void Main(string[] args)
{
FruitFactory ff = new FruitFactory();
//打印(来自父类的):水果有各种吃法。。。
Fruit fA = ff.CreateFruit("Apple");
fA.Eating();
//打印(来自子类的):苹果洗了吃。。。
Apple apple = ff.CreateFruit("Apple") as Apple;
apple.Eating();
Fruit fB = ff.CreateFruit("Banana");
fB.Eating();
Fruit fC = ff.CreateFruit("Orange");
fC.Eating();
}
}
}
作者: XuGang 网名:钢钢 |
出处: http://xugang.cnblogs.com |
声明: 本文版权归作者和博客园共有。转载时必须保留此段声明,且在文章页面明显位置给出原文连接地址! |