首页 > 其他 > 详细

反射动态创建不同的Processor

时间:2014-09-26 18:11:19      阅读:136      评论:0      收藏:0      [点我收藏+]

1. 定义抽象方法

  public abstract class BaseProcesser
    {
        public abstract void GetCustomerReportCardDetailInfo(CustomerReportCardRequest request, ReportCardResult response);
    }

2. 所有Processor都继承自BaseProcessor

 public class GiftProcesser : BaseProcesser
    {
        public override void GetCustomerReportCardDetailInfo(CustomerReportCardRequest request, ReportCardResult response)
        {
            response.GiftInfo = GiftAccessor.GetGiftInfo(request);
        }
    }

3.将Processor的名称配置起来,通过反射动态加载去执行

4.具体示例如下:

 public static CustomerReportCardResponse GetCustomerReportCardInfo(CustomerReportCardRequest request, string view, string[] expand)
        {
            ReportCardResult result = new ReportCardResult();
            List<string> currencyCodes = new List<string>();
            if (view == "DETAIL")
            {
                if (!CheckCurrencyCode(request, ref currencyCodes))
                {
                    return null;
                }

                IEnumerable<string> processKeys = AllProcessers.Where(n => expand.Contains(n.Key)).Select(n => n.Key);
                foreach (var key in processKeys)
                {
                    var processer = AllProcessers[key];
                    processer.GetCustomerReportCardDetailInfo(request, result);
                }
            }
            return SetCustomerReportCardResponse(result, request, currencyCodes);
        }

        private static Dictionary<string, BaseProcesser> allProcessers = null;
        private static Dictionary<string, BaseProcesser> AllProcessers
        {
            get
            {
                if (allProcessers == null)
                {
                    allProcessers = new Dictionary<string, BaseProcesser>();
                    Dictionary<string, string> classNames = GetResource();

                    foreach (var key in classNames.Keys)
                    {
                        BaseProcesser detail = typeof(BaseProcesser).Assembly.CreateInstance(classNames[key]) as BaseProcesser;
                        if (detail == null) continue;

                        if (!allProcessers.ContainsKey(key))
                        {
                            allProcessers.Add(key, detail);
                        }
                    }
                }
                return allProcessers;
            }
        }

        private static Dictionary<string, string> GetResource()
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            PerKeyValue keyValue = ConfigContainer.KeyValuesConfig["CustomerReportCardExpand"];
            if (keyValue == null) return null;

            PerKeyValueCollection keyValues = keyValue.KeyValues;
            string assemblyBase = keyValue.Attributes["Class"].Value;

            foreach (PerKeyValue resource in keyValues)
            {
                if (resource == null) continue;

                result.Add(resource.Name, assemblyBase + "." + resource.Value);
            }
            return result;
        }

  

 

反射动态创建不同的Processor

原文:http://www.cnblogs.com/Wolfmanlq/p/3995014.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!