csharp: InvokeHelper
Entity Framework
https://entityframework.codeplex.com/
Enterprise Library
1 2 3 4 5 6 | <?xml version= "1.0" ?> <configuration> <startup> <supportedRuntime version= "v2.0.50727" /></startup> </configuration> |
App.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespace InvokerHelperDemo { /// <summary> /// http://www.getcodesamples.com/src/E6947A0/4F02A4B8 /// /// </summary> public class Helpers { static private System.Collections.Generic.SortedDictionary< object , DuInvokeHelper> m_List = new SortedDictionary< object , DuInvokeHelper>(); /// <summary> /// /// </summary> /// <param name="Instance"></param> /// <param name="helper"></param> public static void AddHelper( object Instance, DuInvokeHelper helper) { m_List.Add(Instance, helper); } /// <summary> /// /// </summary> /// <param name="Instance"></param> /// <returns></returns> public static DuInvokeHelper find( object Instance) { if (m_List.ContainsKey(Instance)) return m_List[Instance]; else return null ; } } /// <summary> /// /// </summary> public class DuInvokeHelper { static System.Collections.Generic.Dictionary< string , Type> Types = new Dictionary< string , Type>(); protected object m_Instance; Type m_Type; /// <summary> /// /// </summary> public object Instance { get { return m_Instance; } } /// <summary> /// /// </summary> /// <param name="Instance"></param> public DuInvokeHelper( object Instance) { m_Instance = Instance; m_Type = Instance.GetType(); // Helpers.AddHelper(Instance, this); } /// <summary> /// /// </summary> /// <param name="Instance"></param> /// <param name="type"></param> public DuInvokeHelper( object Instance, Type type) { m_Instance = Instance; m_Type = type; } /// <summary> /// /// </summary> /// <param name="FieldName"></param> /// <returns></returns> public bool HasField( string FieldName) { foreach (FieldInfo field in m_Type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { if (field.Name == FieldName) return true ; } return false ; } /// <summary> /// /// </summary> /// <param name="FieldName"></param> /// <returns></returns> public object GetProperty( string FieldName) { return m_Type.InvokeMember(FieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null , this .m_Instance, null ); } /// <summary> /// /// </summary> /// <param name="FieldName"></param> /// <param name="Value"></param> /// <returns></returns> public object SetProperty( string FieldName, object Value) { return m_Type.InvokeMember(FieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, null , this .m_Instance, new object [] { Value }); } /// <summary> /// /// </summary> /// <param name="FieldName"></param> /// <param name="args"></param> /// <returns></returns> public object GetField( string FieldName, object [] args) { return m_Type.InvokeMember(FieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetField, null , this .m_Instance, args); } /// <summary> /// /// </summary> /// <param name="FieldName"></param> /// <param name="args"></param> /// <returns></returns> public object SetField( string FieldName, object [] args) { return m_Type.InvokeMember(FieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetField, null , this .m_Instance, args); } /// <summary> /// /// </summary> /// <param name="FieldName"></param> /// <param name="Value"></param> /// <returns></returns> public object SetField( string FieldName, object Value) { return SetField(FieldName, new object [] { Value }); } /// <summary> /// /// </summary> /// <param name="PropertyName"></param> /// <returns></returns> public object StaticGetProperty( string PropertyName) { return m_Type.InvokeMember(PropertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.GetProperty, null , null , null ); } /// <summary> /// /// </summary> /// <param name="args"></param> /// <returns></returns> public object CreateInstance( object [] args) { return null ; } /// <summary> /// /// </summary> /// <param name="FieldName"></param> /// <returns></returns> public object GetField( string FieldName) { return GetField(FieldName, null ); } /// <summary> /// /// </summary> /// <param name="MethodName"></param> /// <param name="args"></param> /// <param name="modifiers"></param> /// <returns></returns> public object InvokeByRef( string MethodName, ref object [] args, ParameterModifier modifiers) { ParameterModifier[] mod = { modifiers }; return m_Type.InvokeMember(MethodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null , m_Instance, args, mod, null , null ); } /// <summary> /// /// </summary> /// <param name="MethodName"></param> /// <param name="args"></param> /// <param name="modifiers"></param> /// <returns></returns> public object InvokeByOut( string MethodName, object [] args, ParameterModifier modifiers) { ParameterModifier[] mod = { modifiers }; return m_Type.InvokeMember(MethodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null , m_Instance, args, mod, null , null ); } /// <summary> /// /// </summary> /// <param name="MethodName"></param> /// <param name="args"></param> /// <returns></returns> public object Invoke( string MethodName, object [] args) { return m_Type.InvokeMember(MethodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null , m_Instance, args); } /// <summary> /// /// </summary> /// <param name="MethodName"></param> /// <param name="args"></param> /// <returns></returns> public object InvokeStatic( string MethodName, object [] args) { return m_Type.InvokeMember(MethodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null , null , args); } /// <summary> /// /// </summary> /// <param name="MethodName"></param> /// <param name="arg1"></param> /// <param name="arg2"></param> /// <returns></returns> public object InvokeStatic( string MethodName, object arg1, object arg2) { return InvokeStatic(MethodName, new object [] { arg1, arg2 }); } /// <summary> /// /// </summary> /// <param name="MethodName"></param> /// <param name="arg1"></param> /// <returns></returns> public object InvokeStatic( string MethodName, object arg1) { return InvokeStatic(MethodName, new object [] { arg1 }); } /// <summary> /// /// </summary> /// <param name="MethodName"></param> /// <returns></returns> public object InvokeStatic( string MethodName) { return InvokeStatic(MethodName, null ); } /// <summary> /// /// </summary> /// <param name="MethodName"></param> /// <returns></returns> public object Invoke( string MethodName) { return Invoke(MethodName, null ); } /// <summary> /// /// </summary> /// <param name="MethodName"></param> /// <param name="arg1"></param> /// <returns></returns> public object Invoke( string MethodName, object arg1) { return Invoke(MethodName, new object [] { arg1 }); } /// <summary> /// /// </summary> /// <param name="MethodName"></param> /// <param name="arg1"></param> /// <returns></returns> public object Invoke( string MethodName, int arg1) { return Invoke(MethodName, new object [] { arg1 }); } /// <summary> /// /// </summary> /// <param name="MethodName"></param> /// <param name="obj1"></param> /// <param name="obj2"></param> /// <returns></returns> public object Invoke( string MethodName, object obj1, object obj2) { return Invoke(MethodName, new object [] { obj1, obj2 }); } /// <summary> /// /// </summary> /// <param name="MethodName"></param> /// <param name="obj1"></param> /// <param name="obj2"></param> /// <param name="obj3"></param> /// <returns></returns> public object Invoke( string MethodName, object obj1, object obj2, object obj3) { return Invoke(MethodName, new object [] { obj1, obj2, obj3 }); } /// <summary> /// /// </summary> /// <param name="TypeName"></param> /// <returns></returns> public static Type FindType( string TypeName) { if (Types.ContainsKey(TypeName)) return Types[TypeName]; Type ret = null ; foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Module module in assembly.GetModules()) { foreach (Type type in module.GetTypes()) { if (type.FullName == TypeName) { ret = type; Types.Add(TypeName, ret); return ret; } } } } Types.Add(TypeName, ret); return ret; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | /******************************************************************************* * InvokeHelper.cs * A thread-safe control invoker helper class. * ----------------------------------------------------------------------------- * Project:Conmajia.Controls * Author:Conmajia * Url:conmajia@gmail.com * History: * 4th Aug., 2012 * Added support for "Non-control" controls (such as ToolStripItem). * * 4th Aug., 2012 * Initiated. ******************************************************************************/ using System; using System.Collections.Generic; using System.Reflection; using System.Text; using System.Windows.Forms; namespace InvokerHelperDemo { /// <summary> /// A thread-safe control invoker helper class. /// 善用IAsyncResult /// 利用BeginInvoke与EndInvoke完成异步委托方法 /// 线程Thread有一个属性IsBackground,通过把此属性设置为true,就可以把线程设置为后台线程 /// </summary> public class InvokeHelper { #region delegates private delegate object MethodInvoker(Control control, string methodName, params object [] args); private delegate object PropertyGetInvoker(Control control, object noncontrol, string propertyName); private delegate void PropertySetInvoker(Control control, object noncontrol, string propertyName, object value); #endregion #region static methods /// <summary> /// helpers /// </summary> /// <param name="control"></param> /// <param name="noncontrol"></param> /// <param name="propertyName"></param> /// <returns></returns> private static PropertyInfo GetPropertyInfo(Control control, object noncontrol, string propertyName) { if (control != null && ! string .IsNullOrEmpty(propertyName)) { PropertyInfo pi = null ; Type t = null ; if (noncontrol != null ) t = noncontrol.GetType(); else t = control.GetType(); pi = t.GetProperty(propertyName); if (pi == null ) throw new InvalidOperationException( string .Format( "Can't find property {0} in {1}." , propertyName, t.ToString() )); return pi; } else throw new ArgumentNullException( "Invalid argument." ); } /// <summary> /// outlines /// </summary> /// <param name="control"></param> /// <param name="methodName"></param> /// <param name="args"></param> /// <returns></returns> public static object Invoke(Control control, string methodName, params object [] args) { if (control != null && ! string .IsNullOrEmpty(methodName)) if (control.InvokeRequired) return control.Invoke( new MethodInvoker(Invoke), control, methodName, args ); else { MethodInfo mi = null ; if (args != null && args.Length > 0) { Type[] types = new Type[args.Length]; for ( int i = 0; i < args.Length; i++) { if (args[i] != null ) types[i] = args[i].GetType(); } mi = control.GetType().GetMethod(methodName, types); } else mi = control.GetType().GetMethod(methodName); // check method info you get if (mi != null ) return mi.Invoke(control, args); else throw new InvalidOperationException( "Invalid method." ); } else throw new ArgumentNullException( "Invalid argument." ); } /// <summary> /// /// </summary> /// <param name="control"></param> /// <param name="propertyName"></param> /// <returns></returns> public static object Get(Control control, string propertyName) { return Get(control, null , propertyName); } /// <summary> /// /// </summary> /// <param name="control"></param> /// <param name="noncontrol"></param> /// <param name="propertyName"></param> /// <returns></returns> public static object Get(Control control, object noncontrol, string propertyName) { if (control != null && ! string .IsNullOrEmpty(propertyName)) if (control.InvokeRequired) return control.Invoke( new PropertyGetInvoker(Get), control, noncontrol, propertyName ); else { PropertyInfo pi = GetPropertyInfo(control, noncontrol, propertyName); object invokee = (noncontrol == null ) ? control : noncontrol; if (pi != null ) if (pi.CanRead) return pi.GetValue(invokee, null ); else throw new FieldAccessException( string .Format( "{0}.{1} is a write-only property." , invokee.GetType().ToString(), propertyName )); return null ; } else throw new ArgumentNullException( "Invalid argument." ); } /// <summary> /// /// </summary> /// <param name="control"></param> /// <param name="propertyName"></param> /// <param name="value"></param> public static void Set(Control control, string propertyName, object value) { Set(control, null , propertyName, value); } /// <summary> /// /// </summary> /// <param name="control"></param> /// <param name="noncontrol"></param> /// <param name="propertyName"></param> /// <param name="value"></param> public static void Set(Control control, object noncontrol, string propertyName, object value) { if (control != null && ! string .IsNullOrEmpty(propertyName)) if (control.InvokeRequired) control.Invoke( new PropertySetInvoker(Set), control, noncontrol, propertyName, value ); else { PropertyInfo pi = GetPropertyInfo(control, noncontrol, propertyName); object invokee = (noncontrol == null ) ? control : noncontrol; if (pi != null ) if (pi.CanWrite) pi.SetValue(invokee, value, null ); else throw new FieldAccessException( string .Format( "{0}.{1} is a read-only property." , invokee.GetType().ToString(), propertyName )); } else throw new ArgumentNullException( "Invalid argument." ); } #endregion } } |
用例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Threading; using System.Windows.Forms; namespace InvokerHelperDemo { /// <summary> /// /// </summary> public partial class Form1 : Form { /// <summary> /// /// </summary> /// <returns></returns> DataTable setData() { DataTable dt = new DataTable(); dt.Columns.Add( "id" , typeof ( int )); dt.Columns.Add( "name" , typeof ( string )); dt.Columns.Add( "date" , typeof (DateTime)); dt.Rows.Add(1, "ds" , "2015-01-02" ); dt.Rows.Add(2, "sb" , "2015-01-02" ); dt.Rows.Add(3, "sg" , "2015-01-02" ); return dt; } /// <summary> /// /// </summary> public Form1() { InitializeComponent(); } Thread t; /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click( object sender, EventArgs e) { if (t == null ) { t = new Thread(multithread); t.Start(); label4.Text = string .Format( "Thread state:\n{0}" , t.ThreadState.ToString() ); } } /// <summary> /// /// </summary> /// <param name="msg"></param> public void DoWork( string msg) { this .label3.Text = string .Format( "Invoke method: {0}" , msg); } // int count = 0; /// <summary> /// /// </summary> void multithread() { //while (true) //{ InvokeHelper.Set( this .label1, "Text" , string .Format( "Set value: {0}" , count)); InvokeHelper.Set( this .label1, "Tag" , count); InvokeHelper.Set( this .dataGridView1, "DataSource" , setData()); string value = InvokeHelper.Get( this .label1, "Tag" ).ToString(); InvokeHelper.Set( this .label2, "Text" , string .Format( "Get value: {0}" , value)); InvokeHelper.Invoke( this , "DoWork" , value); Thread.Sleep(500); count++; //} } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_FormClosed( object sender, FormClosedEventArgs e) { if (t != null && t.IsAlive) t.Abort(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load( object sender, EventArgs e) { } } } |
http://www.codeproject.com/Articles/29680/Towards-Cleaner-Code-A-C-Asynchronous-Helper
http://www.codeproject.com/Articles/29829/Towards-Cleaner-Code-II-a-C-GUI-Invoke-Async-Helpe
http://www.codeproject.com/Articles/10346/Remoting-with-GUIs
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!