本文由【shakusi 原创】
最近刚换工作,其中项目中用到一个动态创建类的问题,后来研究了微软的ConstructorInfo类解决此问题。其中作了多次尝试,才正确理解此类的用法。
现在作一个例子以便容易理解。
假定一个买苹果的程序是这样:苹果分为红苹果和青苹果,它们在上半年和下半年单价不一样;它们不同产地的价格在不同时间也不一样。输入苹果名称,显示单价,再输入产地,显示此苹果此产地单价。
步骤:
1、在VS2008上新建一个控制台应用程序,名称为InvokeApple
2、新建类Apple.cs(苹果父类)
[csharp] view plaincopyprint?
- 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;
- }
- }
- }
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?
- </pre><pre name="code" class="csharp"> 3、新建类RedApple.cs(红苹果)
</pre><pre name="code" class="csharp"> 3、新建类RedApple.cs(红苹果)
[csharp] view plaincopyprint?
- 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;
- }
- }
- }
- }
- }
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?
- 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;
- }
- }
- }
- }
- }
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?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace InvokeApple
- {
- public interface IBuyApple
- {
- /// <summary>
- /// 获取当前月份
- /// </summary>
- /// <returns></returns>
- int GetMonth();
- }
- }
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?
- 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;
- }
- }
- }
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?
- 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);
- }
- }
- }
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
请按任意键继续. . .
浙公网安备 33010602011771号