C#界說簡略的反射工場實例剖析。本站提示廣大學習愛好者:(C#界說簡略的反射工場實例剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是C#界說簡略的反射工場實例剖析正文
本文實例講述了C#界說簡略的反射工場用法。分享給年夜家供年夜家參考。詳細剖析以下:
起首,界說一個生果籠統類,代碼以下:
class Fruit
{
//界說虛辦法
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("蘋果洗了吃。。。");
//}
}
最初,創立生果工場,代碼以下:
//生果工場
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();
}
}
應用反射創立實例對象的經常使用三種方法:
// 方法一:應用 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;
示例的全體代碼以下:
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();
}
}
}
願望本文所述對年夜家的C#法式設計有所贊助。