/// <summary>根据字段查找指定ID的控件,采用反射字段的方法,避免遍历Controls引起子控件构造</summary> /// <typeparam name="T"></typeparam> /// <param name="control">容器</param> /// <param name="id"></param> /// <returns></returns> public static T FindControlByField <T>(Control control, String id) where T : Control { if (control == null) { return(null); } FieldInfoX fix = null; if (!String.IsNullOrEmpty(id)) { fix = FieldInfoX.Create(control.GetType(), id); } else { Type t = typeof(T); FieldInfo fi = control.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FirstOrDefault(item => item.FieldType == t); if (fi != null) { fix = FieldInfoX.Create(fi); } } if (fix == null) { return(null); } return(fix.GetValue(control) as T); }
static void ShowText(String txtName) { var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(Program).Namespace + "." + txtName); var txt = stream.ToStr(); foreach (var item in txt.Split(new String[] { Environment.NewLine }, StringSplitOptions.None)) { // 改变颜色 if (item.StartsWith("[Color:")) { var name = item.Substring("[Color:".Length); name = name.TrimEnd(']'); var fix = FieldInfoX.Create(typeof(ConsoleColor), name); if (fix != null) { Console.ForegroundColor = (ConsoleColor)fix.GetValue(); } } else if (item == "[Pause]") { Console.ReadKey(true); } else { Console.WriteLine(item); } } }
/// <summary>复制DirEntry专属的字段</summary> /// <param name="entry"></param> internal void CopyFromDirEntry(ZipEntry entry) { Type type = this.GetType(); foreach (var item in dirMembers) { var fix = FieldInfoX.Create(type, item); fix.SetValue(this, fix.GetValue(entry)); } }
/// <summary>获取提供者工厂</summary> /// <param name="assemblyFile"></param> /// <param name="className"></param> /// <returns></returns> protected static DbProviderFactory GetProviderFactory(String assemblyFile, String className) { //反射实现获取数据库工厂 String file = assemblyFile; if (!Runtime.IsWeb) { file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file); } else { file = Path.Combine(HttpRuntime.BinDirectory, file); } if (!File.Exists(file)) { CheckAndDownload(file); // 如果还没有,就写异常 if (!File.Exists(file)) { throw new FileNotFoundException("缺少文件" + file + "!", file); } } Assembly asm = Assembly.LoadFile(file); if (asm == null) { return(null); } Type type = asm.GetType(className); if (type == null) { return(null); } var field = FieldInfoX.Create(type, "Instance"); if (field == null) { return(Activator.CreateInstance(type) as DbProviderFactory); } return(field.GetValue(null) as DbProviderFactory); }
/// <summary>在窗体中查询ToolTip</summary> /// <returns></returns> private ToolTip FindToolTipParentForm() { //在窗里中查找ToolTip实例 ToolTip r = null; if (Container == null) { throw new Exception("Container 为空!"); } //查找窗体 Control form = Container is Form ? Container : Container.FindForm(); if (form == null) { throw new Exception("ToolTip 父窗口查询失败!"); } FieldInfo toolTipField = null; FieldInfo[] fields = form.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); if (fields != null && fields.Length > 0) { Type toolTip = typeof(System.Windows.Forms.ToolTip); foreach (FieldInfo item in fields) { if (item.FieldType == toolTip) { toolTipField = item; break; } } } if (toolTipField != null) { r = FieldInfoX.Create(toolTipField).GetValue(form) as ToolTip; } return(null); }
protected void Page_Load(object sender, EventArgs e) { FieldInfoX fix = FieldInfoX.Create(Page.GetType(), "Manager"); if (fix != null) { IManagePage manager = fix.GetValue(Page) as IManagePage; if (manager != null) { Title = manager.CurrentMenu.Name; //Response.Write(Title); Navigation = manager.Navigation; } } bool IsShowToolBar = DmFramework.Web.WebHelper.RequestBool("SetToolBar"); if (!String.IsNullOrEmpty(Request["SetToolBar"]) && !IsShowToolBar) { SetToolBar(this, IsShowToolBar); } }
/// <summary>查找控件的事件</summary> /// <param name="control"></param> /// <param name="eventName"></param> /// <returns></returns> public static Delegate FindEventHandler(Control control, String eventName) { if (control == null) { return(null); } if (String.IsNullOrEmpty(eventName)) { return(null); } var pix = PropertyInfoX.Create(control.GetType(), "Events"); if (pix == null) { return(null); } EventHandlerList list = pix.GetValue(control) as EventHandlerList; if (list == null) { return(null); } var fix = FieldInfoX.Create(control.GetType(), eventName); if (fix == null && !eventName.StartsWith("Event", StringComparison.Ordinal)) { fix = FieldInfoX.Create(control.GetType(), "Event" + eventName); } if (fix == null) { return(null); } return(list[fix.GetValue(control)]); }
Type GetItemType(String name) { if (String.IsNullOrEmpty(name) || EntityType == null) { return(null); } PropertyInfoX pi = PropertyInfoX.Create(EntityType, name); if (pi != null) { return(pi.Type); } FieldInfoX fi = FieldInfoX.Create(EntityType, name); if (fi != null) { return(fi.Type); } return(null); }
/// <summary>在页面查找指定ID的控件,采用反射字段的方法,避免遍历Controls引起子控件构造</summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <returns></returns> public static T FindControlInPage <T>(String id) where T : Control { if (HttpContext.Current == null) { return(null); } IHttpHandler handler = HttpContext.Current.Handler; if (handler == null) { return(null); } FieldInfoX fix = null; if (!String.IsNullOrEmpty(id)) { fix = FieldInfoX.Create(handler.GetType(), id); } else { Type t = typeof(T); FieldInfo fi = handler.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FirstOrDefault(item => item.FieldType == t); if (fi != null) { fix = FieldInfoX.Create(fi); } } if (fix == null) { return(null); } return(fix.GetValue(handler) as T); }