using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
namespace DynamicAssemblyDemo
{
public class RuleManager
{
List<Rule> rules = new List<Rule>();
public List<Rule> Rules
{
get { return rules; }
set { rules = value; }
}
Assembly assembly = null;
public Assembly Assembly
{
get { return assembly; }
set
{
assembly = value;
if(assembly != null)
bookShopRuleClass = Activator.CreateInstance(assembly.GetType("DynamicAssemblyDemo.DynamicAssembly.BookShopRuleClass"));
}
}
object bookShopRuleClass = null;
public void GerRulesFromTheSever()
{
string tempPath = "Rules.xml";
XmlTextReader reader = new XmlTextReader(tempPath);
reader.WhitespaceHandling = WhitespaceHandling.None;
while (reader.ReadToFollowing("Rule"))
{
reader.ReadStartElement("Rule");
reader.ReadStartElement("Name");
string name = reader.ReadString();
reader.ReadEndElement();
reader.ReadStartElement("Item");
string item = reader.ReadString();
reader.ReadEndElement();
reader.ReadStartElement("ReturnType");
string returnType = reader.ReadString();
reader.ReadEndElement();
reader.ReadStartElement("Body");
string body = reader.ReadString().Replace("And","&&");
reader.ReadEndElement();
Rule r = new Rule(name, body, item, returnType);
rules.Add(r);
}
}
public void SetPublicFields(string cat, string subCat, string item, double price, Int64 quanity)
{
Type ty = bookShopRuleClass.GetType();
ty.GetField(Meta.price).SetValue(bookShopRuleClass, price);
ty.GetField(Meta.quantity).SetValue(bookShopRuleClass, quanity);
ty.GetField(Meta.subCategory).SetValue(bookShopRuleClass, subCat);
ty.GetField(Meta.item).SetValue(bookShopRuleClass, item);
}
public double GetDiscount(string cat , string subCat , string item, double price , Int64 quanity)
{
if (bookShopRuleClass != null)
{
SetPublicFields(cat, subCat, item, price, quanity);
string name = Meta.getDiscount + Meta.sep + cat;
Type type = bookShopRuleClass.GetType();
MethodInfo method = type.GetMethod(name);
if (method != null)
{
object discount = method.Invoke(bookShopRuleClass, null);
return Convert.ToDouble(discount);
}
}
return 0;
}
public string GetSpecialGifts(string cat, string subCat, string item, double price, Int64 quanity)
{
if (bookShopRuleClass != null)
{
SetPublicFields(cat, subCat, item, price, quanity);
string name = Meta.getSpecialGifts + Meta.sep + cat;
Type type = bookShopRuleClass.GetType();
MethodInfo method = type.GetMethod(name);
if (method != null)
{
object gift = method.Invoke(bookShopRuleClass, null);
return gift.ToString();
}
}
return string.Empty;
}
public bool ValidateOrderValue(string cat, string subCat, string item, double price, Int64 quanity)
{
if (bookShopRuleClass != null)
{
SetPublicFields(cat, subCat, item, price, quanity);
string name = Meta.validateOrderValue + Meta.sep + cat;
Type type = bookShopRuleClass.GetType();
MethodInfo method = type.GetMethod(name);
if (method != null)
{
object ret = method.Invoke(bookShopRuleClass, null);
return Convert.ToBoolean(ret);
}
}
return false;
}
public bool ValidateOrderQuantity(string cat, string subCat, string item, double price, Int64 quanity)
{
if (bookShopRuleClass != null)
{
SetPublicFields(cat, subCat, item, price, quanity);
string name = Meta.validateOrderQuantity + Meta.sep + cat;
Type type = bookShopRuleClass.GetType();
MethodInfo method = type.GetMethod(name);
if (method != null)
{
object ret = method.Invoke(bookShopRuleClass, null);
return Convert.ToBoolean(ret);
}
}
return false;
}
}
}