Click here to Skip to main content
15,886,799 members
Articles / Programming Languages / C#

Dynamic Assemblies

Rate me:
Please Sign up or sign in to vote.
2.94/5 (10 votes)
24 Aug 2008CPOL8 min read 37.5K   636   30  
Creating Dynamic assemblies in C#
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;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Sri Lanka Sri Lanka
PhD, BSc(Eng), MCP (Web Development)

Comments and Discussions