反射动态创建不同的Processor

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;
        }

  

原文地址:https://www.cnblogs.com/Wolfmanlq/p/3995014.html