如何从另一个线程获取GUI控件的属性值

发布于 2024-10-20 18:36:41 字数 483 浏览 1 评论 0原文

VS2005 / Framework 2.0 / VB.NET

我正在使用BackgroundWorker 控件来做一些长时间的工作,更新模式进度表单(.ShowDialog())。

我已经设法从 BW DoWork / ProgressChanged 事件SET主表单属性值,甚至调用表单的方法(在反射对象的帮助下http://www.switchonthecode.com/tutorials/csharp-tutorial-using-reflection-to-get-object -信息)。

我唯一不知道该怎么做的就是 GET 主窗体控件的属性返回到 BW 线程。

VS2005 / Framework 2.0 / VB.NET

I'm using a BackgroundWorker control to do some long time work which updates a modal progress form (.ShowDialog()).

I've managed to SET main form properties values from BW DoWork / ProgressChanged events and even invoke form's methods (with a little help from Reflection objects http://www.switchonthecode.com/tutorials/csharp-tutorial-using-reflection-to-get-object-information).

the only thing I don't know how to do is to GET main form's control's properties back to the BW thread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷迭香的记忆 2024-10-27 18:36:42

那么,Reflection API 中的所有 Set 方法都有一个相应的 Get 方法,因此示例中的代码可以是:

MyObject myObjectInstance = new MyObject();
System.Reflection.FieldInfo[] fieldInfo = myObjectType.GetFields();
string strValue = string.Empty;
int intValue = 0;
object objValue = null;

foreach (System.Reflection.FieldInfo info in fieldInfo)
{
   switch (info.Name)
   {
      case "myStringField":
         strValue = (string)info.GetValue(myObjectInstance);
        break;
      case "myIntField":
        intValue = (int)info.GetValue(myObjectInstance);
        break;
     case "myObjectField":
        objValue = info.GetValue(myObjectInstance);
        break;
}

但是,如果您有很多属性,这是获取/设置单个值的低效方法,因此您可以使用GetField 方法,如下所示:

myType = myObjectInstance.GetType();
strValue = (string)myType.GetField("myStringField").GetValue(myObjectInstance);
intValue = (int)myType.GetField("myIntField").GetValue(myObjectInstance);
objValue = myType.GetField("myObjectField").GetValue(myObjectInstance);

还有一件事,Reflection 是一个很棒的工具,但需要付出代价。您的代码的可维护性较差(毕竟,您使用字符串作为字段名称)并且性能受到严重损害。我的底线是尽可能避免反射,所以请首先尝试寻找替代解决方案。

Well, all Set methods in the Reflection API have a corresponding Get method, so the code from the example could be:

MyObject myObjectInstance = new MyObject();
System.Reflection.FieldInfo[] fieldInfo = myObjectType.GetFields();
string strValue = string.Empty;
int intValue = 0;
object objValue = null;

foreach (System.Reflection.FieldInfo info in fieldInfo)
{
   switch (info.Name)
   {
      case "myStringField":
         strValue = (string)info.GetValue(myObjectInstance);
        break;
      case "myIntField":
        intValue = (int)info.GetValue(myObjectInstance);
        break;
     case "myObjectField":
        objValue = info.GetValue(myObjectInstance);
        break;
}

However, this is a inefficient way to get/set a single value if you have lots of properties, so you can use the GetField method, like this:

myType = myObjectInstance.GetType();
strValue = (string)myType.GetField("myStringField").GetValue(myObjectInstance);
intValue = (int)myType.GetField("myIntField").GetValue(myObjectInstance);
objValue = myType.GetField("myObjectField").GetValue(myObjectInstance);

And one more thing, Reflection is a great tool, but there's a price to pay. Your code is less maintainable (after all, you are using string for the field names) and the performance is severely hurt. My bottom line is to try avoiding reflection whenever possible, so please try to look for an alternate solution first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文