C#定義簡單的反射工廠實(shí)例分析
本文實(shí)例講述了C#定義簡單的反射工廠用法。分享給大家供大家參考。具體分析如下:
首先,定義一個水果抽象類,代碼如下:
class Fruit
{
//定義虛方法
public virtual void Eating()
{
Console.WriteLine("水果有各種吃法。。。");
}
}
然后,實(shí)例化幾個水果類,代碼如下:
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("蘋果洗了吃。。。");
//}
}
最后,創(chuàng)建水果工廠,代碼如下:
//水果工廠
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("您指定的水果不生產(chǎn)!");
//}
//獲得當(dāng)前程序的命名空間
string strNamespace = Assembly.GetExecutingAssembly().GetName().Name;
//調(diào)用方法一:使用 Type 類
//Type type = Type.GetType("ConsoleApplication1." + _fruitname);
//ConstructorInfo ctorInfo = type.GetConstructor(System.Type.EmptyTypes);
//// Invoke()方法:返回與構(gòu)造函數(shù)關(guān)聯(lián)的類的實(shí)例。
//Fruit fruitA = (Fruit)ctorInfo.Invoke(new object[0]);
//return fruitA;
//調(diào)用方法二:使用 Assembly 類
//Assembly myAssembly = Assembly.GetExecutingAssembly();
//Fruit fruitB = (Fruit)myAssembly.CreateInstance("ConsoleApplication1." + _fruitname);
//return fruitB;
//調(diào)用方法三:使用 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();
}
}
利用反射創(chuàng)建實(shí)例對象的常用三種方式:
// 方式一:使用 Type 類
Type type = Type.GetType("ConsoleApplication1." + _fruitname);
ConstructorInfo ctorInfo = type.GetConstructor(System.Type.EmptyTypes);
// Invoke()方法:返回與構(gòu)造函數(shù)關(guān)聯(lián)的類的實(shí)例。
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("您指定的水果不生產(chǎn)!");
//}
//獲得當(dāng)前程序的命名空間
string strNamespace = Assembly.GetExecutingAssembly().GetName().Name;
//調(diào)用方法一:使用 Type 類
//Type type = Type.GetType("ConsoleApplication1." + _fruitname);
//ConstructorInfo ctorInfo = type.GetConstructor(System.Type.EmptyTypes);
//// Invoke()方法:返回與構(gòu)造函數(shù)關(guān)聯(lián)的類的實(shí)例。
//Fruit fruitA = (Fruit)ctorInfo.Invoke(new object[0]);
//return fruitA;
//調(diào)用方法二:使用 Assembly 類
//Assembly myAssembly = Assembly.GetExecutingAssembly();
//Fruit fruitB = (Fruit)myAssembly.CreateInstance("ConsoleApplication1." + _fruitname);
//return fruitB;
//調(diào)用方法三:使用 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();
//打?。▉碜愿割惖模核懈鞣N吃法。。。
Fruit fA = ff.CreateFruit("Apple");
fA.Eating();
//打?。▉碜宰宇惖模禾O果洗了吃。。。
Apple apple = ff.CreateFruit("Apple") as Apple;
apple.Eating();
Fruit fB = ff.CreateFruit("Banana");
fB.Eating();
Fruit fC = ff.CreateFruit("Orange");
fC.Eating();
}
}
}
希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
Unity實(shí)現(xiàn)粒子光效導(dǎo)出成png序列幀
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)粒子光效導(dǎo)出成png序列幀,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03
在Form_Load里面調(diào)用Focus無效的解決方法
在調(diào)用Form_Load的時候,F(xiàn)orm其實(shí)還沒有進(jìn)入展示階段,自然Focus()調(diào)用也就沒效果了。2013-02-02
C# linq查詢之動態(tài)OrderBy用法實(shí)例
這篇文章主要介紹了C# linq查詢之動態(tài)OrderBy用法,實(shí)例分析了C#采用linq方式查詢時動態(tài)排序的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Unity中Instantiate實(shí)例化物體卡頓問題的解決
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)離線計(jì)時器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-10-10
測試框架nunit之a(chǎn)ssertion斷言使用詳解
NUnit是.Net平臺的測試框架,廣泛用于.Net平臺的單元測試和回歸測試中,下面我們用示例詳細(xì)學(xué)習(xí)一下他的使用方法2014-01-01
c# 實(shí)現(xiàn)康威生命游戲(細(xì)胞自動機(jī))的示例
這篇文章主要介紹了c# 實(shí)現(xiàn)康威生命游戲(細(xì)胞自動機(jī))的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02
WPF拖動DataGrid滾動條時內(nèi)容混亂的解決方法
這篇文章主要介紹了WPF拖動DataGrid滾動條時內(nèi)容混亂的解決方法2016-10-10

