国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#定義簡單的反射工廠實例分析

2020-01-24 02:02:57
字體:
來源:轉載
供稿:網友

本文實例講述了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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通辽市| 黄浦区| 手机| 攀枝花市| 会宁县| 壶关县| 集贤县| 永吉县| 孝昌县| 宁晋县| 黄山市| 海伦市| 盐边县| 唐海县| 苏尼特右旗| 师宗县| 保靖县| 马龙县| 弋阳县| 星子县| 枝江市| 黄龙县| 宕昌县| 利川市| 连城县| 资中县| 花莲县| 盐津县| 钦州市| 新宾| 文昌市| 航空| 环江| 米林县| 苏尼特左旗| 贺州市| 台北市| 中宁县| 勐海县| 庐江县| 紫金县|