csharp: InvokeHelper
Entity Framework
https://entityframework.codeplex.com/
Enterprise Library
<?xml version="1.0"?>
<configuration> <startup>
<supportedRuntime version="v2.0.50727"/></startup>
</configuration>
App.config
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; } } }
/*******************************************************************************
* 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
}
}
用例:
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
csharp: InvokeHelper的更多相关文章
- c#操作MangoDB 之MangoDB CSharp Driver驱动详解
序言 MangoDB CSharp Driver是c#操作mongodb的官方驱动. 官方Api文档:http://api.mongodb.org/csharp/2.2/html/R_Project_ ...
- c#进阶之神奇的CSharp
CSharp 简写为c#,是一门非常年轻而又有活力的语言. CSharp的诞生 在2000年6月微软发布了c#这门新的语言.作为微软公司.NET 平台的主角,c#吸收了在他之前诞生的语言(c ...
- WindowsCE project missing Microsoft.CompactFramework.CSharp.targets in Visual Studio 2008
00x0 前言 之前在Windows 7系统中开发的WindowsCE项目,最近换成Windows 10系统,需要将项目进行修改,打开项目后提示如下错误: 无法读取项目文件"App.cspr ...
- csharp: Oracle Stored Procedure DAL using ODP.NET
paging : http://www.codeproject.com/Articles/44858/Custom-Paging-GridView-in-ASP-NET-Oracle https:// ...
- Excel转Json,Json转CSharp
一份给策划最好的礼物!就是:Excel2Json2CSharp 策划配置Excel,动不动就要改数值啊,增加字段啊. 程序这边对应的解析类就得改动啊.整一个麻烦了得! 所以我就整理了这个Excel2J ...
- Microsoft.CompactFramework.CSharp.targets not found
今天打开VS2008的智能设备项目,报以下错误,应该是文件找不到了. The imported project "C:\WINDOWS\Microsoft.NET\Framework\v3. ...
- CSharp 相关知识点小结
1.JS获取iframe下面的内容document.getElementById('IFRAME1').contentDocument; 2.dialog 弹出层,定位:postion:'bottom ...
- 自己动手制作CSharp编译器
在你喜欢的位置(如F盘根目录)新建一个文件夹,并命名为“CSharp开发环境”.找到或下载C#编译器组件(csc.exe和cscui.exe),并放在先前建立的文件夹中.该组件的一般位置在C盘的.NE ...
- Storm系列(二):使用Csharp创建你的第一个Storm拓扑(wordcount)
WordCount在大数据领域就像学习一门语言时的hello world,得益于Storm的开源以及Storm.Net.Adapter,现在我们也可以像Java或Python一样,使用Csharp创建 ...
随机推荐
- 用Unity模仿CSGO里的火焰效果
CSGO里的火焰效果和真实的情况比较像,能沿着遮挡物前进,如下是模仿效果. 思路比较简单,开始想的是一圈一圈发出去,但是前圈与后圈的联系不好做,换种思路,每个方向发射一条线,这样根据上一个位置的方位先 ...
- 阿里云centos试用
今天体验了下阿里云的centos,起初用的官方推荐的putty方式来管理,全部使用命令行管理起来还是很别扭的. 后来通过百度了解到winscp,有了这个工具,管理起来就爽很多啦.整个centos的管理 ...
- 刨根问底U3D---从Profile中窥探Unity的内存管理
这篇文章包含哪些内容 这篇文章从Unity的Profile组件入手,来探讨一下Unity在开发环境和正式环境中的内存使用发面的一些区别, 并且给出了最好控制内存的方法(我想你已经知道了...Prefa ...
- windows 获取以及更改CMD控制台编码[转]
本文转自 http://blog.sina.com.cn/s/blog_794b1d96010136yy.html 命令 chcp 功能:显示或设置活动代码页编号 CHCP [nnn] nnn ...
- js深拷贝和浅拷贝
一.数组的深浅拷贝 在使用JavaScript对数组进行操作的时候,我们经常需要将数组进行备份,事实证明如果只是简单的将它赋予其他变量,那么我们只要更改其中的任何一个,然后其他的也会跟着改变,这就导致 ...
- LiveWriter Test
From LiveWriter.
- java攻城狮之路(Android篇)--widget_webview_metadata_popupwindow_tabhost_分页加载数据_菜单
一.widget:桌面小控件1 写一个类extends AppWidgetProvider 2 在清单文件件中注册: <receiver android:name=".ExampleA ...
- Scene视图辅助线绘制
有时候需要在Scene视图中绘制一些辅助线,方便进行一些编辑的工作,可以通过如下类和函数完成: 绘制辅助线,相关类: Gizmos类:用于在Scene视图中绘制调试信息或辅助线,这些辅助线只有在Sce ...
- MMM和MHA的对比和应用(PPT分享)
分享主题:MySQL高可用架构 --- MMM&MHA在大众点评应用和改进 内容简介:本次演讲,主要讲述以下几个方面 1. MMM在点评网是如何使用的 2. 细数MMM上踩过的坑以及如何填坑 ...
- I'm back for Machine Learning
Hi, Long time no see. Briefly, I plan to step into this new area, data analysis. In the past few yea ...