/// <summary> /// Registers an instance of an extension created by the given factory method. /// </summary> /// <typeparam name="TExtensionType"> /// The type of the extension. /// </typeparam> /// <param name="factory"> /// The factory method with which the extension can be created. /// </param> /// <returns> /// Instance of the ExtensionPoint with which the extension can be configured furthermore. /// </returns> public ExtensionPoint <TTargetType> With <TExtensionType>(Func <TExtensionType> factory) where TExtensionType : IExtend <TTargetType> { this.Extension = factory.Invoke(); return(this); }
/// <summary> /// 就想看看电影 听听歌 /// iphone ipad itouch /// </summary> /// <param name="phone"></param> public void MovieMusic(IExtend phone) { phone.Movie(); phone.Music(); //phone. }
/// <summary> /// Creates an instance of the given extension type. /// </summary> /// <typeparam name="TExtensionType">The type of the extension.</typeparam> /// <returns> /// Instance of the ExtensionPoint with which the extension can be configured furthermore. /// </returns> /// <exception cref="System.InvalidOperationException">No default constructor found. Extensions need default constructors or must be registered with a factory.</exception> public ExtensionPoint <TTargetType> With <TExtensionType>() where TExtensionType : IExtend <TTargetType> { var type = typeof(TExtensionType); this.Extension = Activator.CreateInstance(type) as IExtend <TTargetType>; return(this); }
/// <summary>针对指定实体对象计算url,替换其中变量</summary> /// <param name="data"></param> /// <returns></returns> public String GetUrl(IExtend data) { if (Url.IsNullOrEmpty()) { return(null); } return(_reg.Replace(Url, m => data[m.Groups[1].Value + ""] + "")); }
/// <summary>针对指定实体对象计算DisplayName,替换其中变量</summary> /// <param name="data"></param> /// <returns></returns> public String GetDisplayName(IExtend data) { if (DisplayName.IsNullOrEmpty()) { return(null); } return(_reg.Replace(DisplayName, m => data[m.Groups[1].Value + ""] + "")); }
/// <summary>扩展接口转名值字典</summary> /// <param name="extend"></param> /// <param name="throwOnError">出错时是否抛出异常</param> /// <returns></returns> public static IDictionary <String, Object> ToDictionary(this IExtend extend, Boolean throwOnError = true) { // 泛型字典 if (extend is IDictionary <String, Object> dictionary) { return(dictionary); } if (extend is ExtendDictionary edic) { return(edic.Items); } if (extend is IExtend3 ext3) { return(ext3.Items); } // IExtend2 if (extend is IExtend2 ext2) { var dic = new Dictionary <String, Object>(); foreach (var item in ext2.Keys) { dic[item] = extend[item]; } return(dic); } // 普通字典 if (extend is IDictionary dictionary2) { var dic = new Dictionary <String, Object>(); foreach (DictionaryEntry item in dictionary2) { dic[item.Key + ""] = item.Value; } return(dic); } // 反射 Items var pi = extend.GetType().GetProperty("Items", BindingFlags.Instance | BindingFlags.Public /*| BindingFlags.NonPublic*/); if (pi != null && pi.PropertyType.As <IDictionary <String, Object> >()) { return(pi.GetValue(extend, null) as IDictionary <String, Object>); } //var fi = extend.GetType().GetField("Items", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); //if (fi != null && fi.FieldType.As<IDictionary<String, Object>>()) return fi.GetValue(extend) as IDictionary<String, Object>; if (throwOnError) { throw new NotSupportedException($"不支持从类型[{extend.GetType().FullName}]中获取字典!"); } return(null); }
private static void Add(Type type, IExtend extension) { lock (RegisteredExtensions) { if (!RegisteredExtensions.ContainsKey(type)) { RegisteredExtensions.Add(type, new List <IExtend>()); } RegisteredExtensions[type].Add(extension); } }
/// <summary>从源对象拷贝数据到目标对象</summary> /// <param name="target">目标对象</param> /// <param name="source">源对象</param> public static void Copy(this IExtend target, IExtend source) { var dst = target.ToDictionary(); var src = source.ToDictionary(); foreach (var item in src) { if (dst.ContainsKey(item.Key)) { dst[item.Key] = item.Value; } } }
public static void AdjustStep(IJob job, JobTask ji, IExtend ext) { // 不许动态调节步进 if (job.StepRate == 0 || job.MinStep == job.MaxStep) { return; } //// 向前调节步进,避免多线程时间错乱 //var dt = ext["Step_Last"].ToDateTime(); //if (dt.Year > 2000 && ji.Start <= dt) return; //ext["Step_Last"] = ji.Start; // 新步进 var st = 0L; var near = false; var adjust = false; // 计算停止线,需要注意偏移量 var end = job.End; if (end.Year <= 2000 || end > DateTime.Now) { end = DateTime.Now.AddSeconds(-job.Offset); } // 距离目标较近时步进减半 if (ji.End.AddSeconds(ji.Step) > end) { st = ji.Step; while (st > 0 && ji.End.AddSeconds(st) > end) { st /= 2; } near = true; } else { if (ji.Total > 0) { // 逼近批大小 st = (Int64)ji.BatchSize * ji.Step / ji.Total; adjust = true; } // 踩空逻辑 else { // 距离目标较远时加大步进 //st = job.MaxStep; st = (Int32)(ji.Step * (1 + job.StepRate / 100.0)); } // 步进率 var jstep = ji.Step; var rate = jstep == 0 ? 0 : (st - jstep) * 100.0 / jstep; // 变化不能超过比率 if (Math.Abs(rate) > job.StepRate) { rate = job.StepRate; if (st < jstep) { rate = -rate; } st = (Int32)(jstep + rate * jstep / 100); } } // 限制最大最小步进 if (st < job.MinStep) { st = job.MinStep; } else if (st > job.MaxStep) { st = job.MaxStep; } if (st == job.Step) { return; } // 踩空时要求步进没有改变过 if (near || adjust || job.Step == ji.Step) { job.Step = (Int32)st; } }
/// <summary>扩展接口转名值字典</summary> /// <remarks> /// 需要注意,还有一个 Object.ToDictionary() 扩展,位于 CollectionHelper /// </remarks> /// <param name="extend">扩展对象</param> /// <returns></returns> public static IDictionary <String, Object> ToDictionary(this IExtend extend) { if (extend == null) { return(null); } // 泛型字典 if (extend is IDictionary <String, Object> dictionary) { return(dictionary); } if (extend is ExtendDictionary edic) { return(edic.Items); } if (extend is IExtend3 ext3) { return(ext3.Items); } // IExtend2 if (extend is IExtend2 ext2) { var dic = new Dictionary <String, Object>(); foreach (var item in ext2.Keys) { dic[item] = extend[item]; } return(dic); } // 普通字典 if (extend is IDictionary dictionary2) { var dic = new Dictionary <String, Object>(); foreach (DictionaryEntry item in dictionary2) { dic[item.Key + ""] = item.Value; } return(dic); } // 反射 Items var pis = extend.GetType().GetProperties(true); var pi = pis.FirstOrDefault(e => e.Name == "Items"); if (pi != null && pi.PropertyType.As <IDictionary <String, Object> >()) { return(pi.GetValue(extend, null) as IDictionary <String, Object>); } // 反射属性 return(new ExtendDictionary2 { Data = extend, Keys = pis.Select(e => e.Name).ToList() }); //var dic2 = new Dictionary<String, Object>(); //foreach (var item in pis) //{ // dic2[item.Name] = extend[item.Name]; //} //return dic2; }
/// <summary>设置第二数据源</summary> /// <param name="dictionary"></param> public void SetData2(IDictionary <String, Object> dictionary) => Data2 = dictionary.ToExtend();
//overloaded to extend an interface type static public void ExtensionMethod(this IExtend exInterface) { Console.WriteLine("Method extended for interface."); }