Policy mode (reflection) and policy mode reflection
Improved reflection policy Mode
Public abstract class CashSpuer
{
Public abstract double acceptCash (double money );
}
Public class CashNormal
{
Public override double acceptCash (double money)
{
Return money;
}
}
Public class CashRebate: CashSpuer
{
Private double moneyRebate = 1d;
Public CashRebate (string moneyRebate)
{
This. moneyRebate = double. Parse (moneyRebate );
}
Public override double acceptCash (double money)
{
Return money * moneyRebate;
}
}
Public class CashReturn: CashSpuer
{
Private double moneyCondition;
Private double moneyReturn;
Public CashReturn (string moneyCondition, string moneyReturn)
{
This. moneyCondition = double. Parse (moneyCondition );
This. moneyReturn = double. Parse (moneyReturn );
}
Public override double acceptCash (double money)
{
If (money> moneyCondition)
{
Return money-Math. Floor (money/moneyCondition) * moneyReturn;
}
Else
Return money;
}
}
Public class CashContext
{
Private CashSpuer cs;
Public void setBehavior (CashSpuer cs)
{
This. cs = cs;
}
Public double acceptCash (double money)
{
Return cs. acceptCash (money );
}
}
}
DataSet ds = null;
Double total = 0d;
Private void btnOK_Click (object sender, EventArgs e)
{
CashContext cc = new CashContext ();
DataRow dr = (DataRow []) ds. tables [0]. select ("name = '" + cbxType. selectedItem. toString () + "'") [0];
Object [] args = null;
If (dr ["para"]. ToString (). Trim ()! = "")
{
Args = dr ["para"]. ToString (). Split (',');
}
String strName = System. IO. Path. GetFileNameWithoutExtension (Assembly. GetExecutingAssembly (). Location );
Cc. setBehavior (CashSpuer) Assembly. load ("WindowsFormsApplication2 "). createInstance ("WindowsFormsApplication2." + dr ["class"], false, BindingFlags. default, null, args, null, null ));
Double totalPrice = 0d;
TotalPrice = cc. acceptCash (Convert. ToDouble (txtPrice. Text) * Convert. ToDouble (txtNums. Text ));
Total = total + totalPrice;
ListBox1.Items. add ("unit price:" + txtPrice. text + "Quantity:" + txtNums. text + "" + cbxType. selectedItem + "Total:" + totalPrice. toString ());
}
Private void Form1_Load (object sender, EventArgs e)
{
Ds = new DataSet ();
Ds. ReadXml (Application. StartupPath + "\ CashAcceptType. xml ");
Foreach (DataRowView dr in ds. Tables [0]. DefaultView)
{
CbxType. Items. Add (dr ["name"]. ToString ());
}
CbxType. SelectedIndex = 0;
}