蓝色信仰

博客园 首页 新随笔 联系 订阅 管理

    本文由【shakusi 原创】

    最近刚换工作,其中项目中用到一个动态创建类的问题,后来研究了微软的ConstructorInfo类解决此问题。其中作了多次尝试,才正确理解此类的用法。

    现在作一个例子以便容易理解。

    假定一个买苹果的程序是这样:苹果分为红苹果和青苹果,它们在上半年和下半年单价不一样;它们不同产地的价格在不同时间也不一样。输入苹果名称,显示单价,再输入产地,显示此苹果此产地单价。

    步骤:

    1、在VS2008上新建一个控制台应用程序,名称为InvokeApple

    2、新建类Apple.cs(苹果父类)

[csharp] view plaincopyprint?

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Reflection;  
  6. namespace InvokeApple  
  7. {  
  8. /// <summary>
  9. /// 基类
  10. /// </summary>
  11. public class Apple  
  12.     {  
  13. public string _strName;  
  14. public double _dPrice;  
  15. public string strName  
  16.         {  
  17. get
  18.             {  
  19. return this._strName;  
  20.             }  
  21. set
  22.             {  
  23.                 _strName = value;  
  24.             }  
  25.         }  
  26. public double dPrice  
  27.         {  
  28. get
  29.             {  
  30. return this._dPrice;  
  31.             }  
  32. set
  33.             {  
  34.                 _dPrice = value;  
  35.             }  
  36.         }  
  37. /// <summary>
  38. /// 通过输入苹果的名称生成所需的苹果类
  39. /// </summary>
  40. /// <param name="childType"></param>
  41. /// <param name="iba"></param>
  42. /// <returns></returns>
  43. public static Apple CreateApple(Type childType, IBuyApple iba)  
  44.         {  
  45.             ConstructorInfo ci = childType.GetConstructor(new Type[] { typeof(IBuyApple) });  
  46.             Apple apple = (Apple)ci.Invoke(new object[] { iba });  
  47. return  apple;  
  48.         }  
  49. /// <summary>
  50. /// 通过输入苹果的名称和产地生成所需的苹果类
  51. /// </summary>
  52. /// <param name="childType"></param>
  53. /// <param name="iba"></param>
  54. /// <param name="strOrigin"></param>
  55. /// <returns></returns>
  56. public static Apple CreateApple(Type childType, IBuyApple iba, string strOrigin)  
  57.         {  
  58.             ConstructorInfo ci = childType.GetConstructor(new Type[] { typeof(IBuyApple), typeof(string) });  
  59.             Apple apple = (Apple)ci.Invoke(new object[] { iba, strOrigin });  
  60. return apple;  
  61.         }  
  62.     }  

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace InvokeApple { /// <summary> /// 基类 /// </summary> public class Apple { public string _strName; public double _dPrice; public string strName { get { return this._strName; } set { _strName = value; } } public double dPrice { get { return this._dPrice; } set { _dPrice = value; } } /// <summary> /// 通过输入苹果的名称生成所需的苹果类 /// </summary> /// <param name="childType"></param> /// <param name="iba"></param> /// <returns></returns> public static Apple CreateApple(Type childType, IBuyApple iba) { ConstructorInfo ci = childType.GetConstructor(new Type[] { typeof(IBuyApple) }); Apple apple = (Apple)ci.Invoke(new object[] { iba }); return apple; } /// <summary> /// 通过输入苹果的名称和产地生成所需的苹果类 /// </summary> /// <param name="childType"></param> /// <param name="iba"></param> /// <param name="strOrigin"></param> /// <returns></returns> public static Apple CreateApple(Type childType, IBuyApple iba, string strOrigin) { ConstructorInfo ci = childType.GetConstructor(new Type[] { typeof(IBuyApple), typeof(string) }); Apple apple = (Apple)ci.Invoke(new object[] { iba, strOrigin }); return apple; } } }

[csharp] view plaincopyprint?

  1. </pre><pre name="code" class="csharp">    3、新建类RedApple.cs(红苹果) 

</pre><pre name="code" class="csharp">    3、新建类RedApple.cs(红苹果)

[csharp] view plaincopyprint?

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace InvokeApple  
  6. {  
  7. /// <summary>
  8. /// 红苹果
  9. /// </summary>
  10. public class RedApple : Apple  
  11.     {  
  12. public RedApple(IBuyApple iba)  
  13.         {  
  14. int i = iba.GetMonth();  
  15.             strName = "红苹果";  
  16. if (i >= 7)  
  17.             {  
  18.                 dPrice = 8.5;  
  19.             }  
  20. else
  21.             {  
  22.                 dPrice = 13.5;  
  23.             }  
  24.         }  
  25. /// <summary>
  26. /// 
  27. /// </summary>
  28. /// <param name="iba"></param>
  29. /// <param name="strOrigin">产地</param>
  30. public RedApple(IBuyApple iba, string strOrigin)  
  31.         {  
  32. int i = iba.GetMonth();  
  33.             strName = "红苹果";  
  34. if (i >= 7)  
  35.             {  
  36. if (strOrigin == "山东")  
  37.                 {  
  38.                     dPrice = 9.5;  
  39.                 }  
  40. else
  41. if (strOrigin == "山南")  
  42.                     {  
  43.                         dPrice = 11.5;  
  44.                     }  
  45. else
  46.                     {  
  47.                         dPrice = 10.5;  
  48.                     }  
  49.             }  
  50. else
  51.             {  
  52. if (strOrigin == "山东")  
  53.                 {  
  54.                     dPrice = 14.5;  
  55.                 }  
  56. else
  57. if (strOrigin == "山南")  
  58.                     {  
  59.                         dPrice = 17.5;  
  60.                     }  
  61. else
  62.                     {  
  63.                         dPrice = 15.5;  
  64.                     }  
  65.             }  
  66.         }  
  67.     }  

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace InvokeApple { /// <summary> /// 红苹果 /// </summary> public class RedApple : Apple { public RedApple(IBuyApple iba) { int i = iba.GetMonth(); strName = "红苹果"; if (i >= 7) { dPrice = 8.5; } else { dPrice = 13.5; } } /// <summary> /// /// </summary> /// <param name="iba"></param> /// <param name="strOrigin">产地</param> public RedApple(IBuyApple iba, string strOrigin) { int i = iba.GetMonth(); strName = "红苹果"; if (i >= 7) { if (strOrigin == "山东") { dPrice = 9.5; } else if (strOrigin == "山南") { dPrice = 11.5; } else { dPrice = 10.5; } } else { if (strOrigin == "山东") { dPrice = 14.5; } else if (strOrigin == "山南") { dPrice = 17.5; } else { dPrice = 15.5; } } } } }     4、新建类GreenApple.cs(青苹果子类)

[csharp] view plaincopyprint?

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace InvokeApple  
  6. {   
  7. /// <summary>
  8. /// 青苹果
  9. /// </summary>
  10. public class GreenApple : Apple  
  11.     {   
  12. public GreenApple(IBuyApple iba)  
  13.         {  
  14. int i = iba.GetMonth();  
  15.             strName = "青苹果";  
  16. if (i >= 7)  
  17.             {  
  18.                 dPrice = 4.5;  
  19.             }  
  20. else
  21.             {  
  22.                 dPrice = 5.5;  
  23.             }  
  24.         }  
  25. /// <summary>
  26. /// 
  27. /// </summary>
  28. /// <param name="iba"></param>
  29. /// <param name="strOrigin">产地</param>
  30. public GreenApple(IBuyApple iba, string strOrigin)  
  31.         {  
  32. int i = iba.GetMonth();  
  33.             strName = "青苹果";  
  34. if (i >= 7)  
  35.             {  
  36. if (strOrigin == "山东")  
  37.                 {  
  38.                     dPrice = 3.5;  
  39.                 }  
  40. else
  41. if (strOrigin == "山南")  
  42.                     {  
  43.                         dPrice = 6.25;  
  44.                     }  
  45. else
  46.                     {  
  47.                         dPrice = 4.7;  
  48.                     }  
  49.             }  
  50. else
  51.             {  
  52. if (strOrigin == "山东")  
  53.                 {  
  54.                     dPrice = 6.3;  
  55.                 }  
  56. else
  57. if (strOrigin == "山南")  
  58.                     {  
  59.                         dPrice = 7.2;  
  60.                     }  
  61. else
  62.                     {  
  63.                         dPrice = 6.5;  
  64.                     }  
  65.             }  
  66.         }  
  67.     }  

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace InvokeApple { /// <summary> /// 青苹果 /// </summary> public class GreenApple : Apple { public GreenApple(IBuyApple iba) { int i = iba.GetMonth(); strName = "青苹果"; if (i >= 7) { dPrice = 4.5; } else { dPrice = 5.5; } } /// <summary> /// /// </summary> /// <param name="iba"></param> /// <param name="strOrigin">产地</param> public GreenApple(IBuyApple iba, string strOrigin) { int i = iba.GetMonth(); strName = "青苹果"; if (i >= 7) { if (strOrigin == "山东") { dPrice = 3.5; } else if (strOrigin == "山南") { dPrice = 6.25; } else { dPrice = 4.7; } } else { if (strOrigin == "山东") { dPrice = 6.3; } else if (strOrigin == "山南") { dPrice = 7.2; } else { dPrice = 6.5; } } } } }     5、新建一个接口IBuyApple.cs(计算当前月份)

[csharp] view plaincopyprint?

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace InvokeApple  
  6. {  
  7. public interface IBuyApple  
  8.     {  
  9. /// <summary>
  10. /// 获取当前月份
  11. /// </summary>
  12. /// <returns></returns>
  13. int GetMonth();  
  14.     }  

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace InvokeApple { public interface IBuyApple { /// <summary> /// 获取当前月份 /// </summary> /// <returns></returns> int GetMonth(); } }
    6、新建买苹果方法类BuyApple.cs(计算当前月份)

[csharp] view plaincopyprint?

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace InvokeApple  
  6. {  
  7. public class BuyApple : IBuyApple  
  8.     {  
  9. public int GetMonth()  
  10.         {  
  11. return DateTime.Now.Month;  
  12.         }  
  13.     }  

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace InvokeApple { public class BuyApple : IBuyApple { public int GetMonth() { return DateTime.Now.Month; } } }
    7、主程序Program.cs

[csharp] view plaincopyprint?

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace InvokeApple  
  6. {  
  7. class Program  
  8.     {  
  9. static void Main(string[] args)  
  10.         {  
  11.             Console.WriteLine("您要买什么苹果?");  
  12.             Console.WriteLine("请输入:");  
  13. string s = Console.ReadLine();  
  14. //需要创建的类
  15.             Type t1;  
  16. string strp = "";  
  17. if (s.Equals("红苹果"))  
  18.             {  
  19.                 t1 = typeof(RedApple);  
  20.             }  
  21. else
  22.             {  
  23.                 t1 = typeof(GreenApple);  
  24.             }  
  25.             BuyApple ba = new BuyApple();  
  26. //产生所需类
  27.             Apple ap = Apple.CreateApple(t1, ba);  
  28. if (s.Equals("红苹果"))  
  29.             {  
  30.                 strp = ((InvokeApple.RedApple)(ap))._dPrice.ToString();  
  31.             }  
  32. else
  33.             {  
  34.                 strp = ((InvokeApple.GreenApple)(ap))._dPrice.ToString();  
  35.             }  
  36.             Console.WriteLine("全国平均单价:" + strp);  
  37. //加上产地
  38.             Console.WriteLine("请输入产地:");  
  39. string s1 = Console.ReadLine();  
  40.             ap = Apple.CreateApple(t1, ba, s1);  
  41. if (s.Equals("红苹果"))  
  42.             {  
  43.                 strp = ((InvokeApple.RedApple)(ap))._dPrice.ToString();  
  44.             }  
  45. else
  46.             {  
  47.                 strp = ((InvokeApple.GreenApple)(ap))._dPrice.ToString();  
  48.             }  
  49.             Console.WriteLine(s1 + "单价:" + strp);  
  50.         }  
  51.     }  

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace InvokeApple { class Program { static void Main(string[] args) { Console.WriteLine("您要买什么苹果?"); Console.WriteLine("请输入:"); string s = Console.ReadLine(); //需要创建的类 Type t1; string strp = ""; if (s.Equals("红苹果")) { t1 = typeof(RedApple); } else { t1 = typeof(GreenApple); } BuyApple ba = new BuyApple(); //产生所需类 Apple ap = Apple.CreateApple(t1, ba); if (s.Equals("红苹果")) { strp = ((InvokeApple.RedApple)(ap))._dPrice.ToString(); } else { strp = ((InvokeApple.GreenApple)(ap))._dPrice.ToString(); } Console.WriteLine("全国平均单价:" + strp); //加上产地 Console.WriteLine("请输入产地:"); string s1 = Console.ReadLine(); ap = Apple.CreateApple(t1, ba, s1); if (s.Equals("红苹果")) { strp = ((InvokeApple.RedApple)(ap))._dPrice.ToString(); } else { strp = ((InvokeApple.GreenApple)(ap))._dPrice.ToString(); } Console.WriteLine(s1 + "单价:" + strp); } } }
运行结果:

您要买什么苹果?
请输入:
红苹果
全国平均单价:8.5
请输入产地:
山南
山南单价:11.5
请按任意键继续. . .

posted on 2012-05-18 14:16  蓝色信仰  阅读(1583)  评论(0)    收藏  举报