获取非公开成员的值 [英] get the value of non public member

查看:104
本文介绍了获取非公开成员的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何开始使用OPCDA.NET在C#

的OPCGroup的非公共成员的价值

 私人无效DataChangeHandler(对象发件人,DataChangeEventArgs E)
       {
           尝试
           {
               obj对象=发送;
               OpcGroup GRP =(OpcGroup)发件人; //我想OPCgroup非公共成员值

               如果(InvokeRequired)
               {
                   的BeginInvoke(新DataChangeEventHandler(DataChangeHandler),新的对象[] {发送,E});
                   //返回;
               }
               如果(parameter.X == 0)
               {
                   parameter.X = 1;
                   主题日=新主题(param.update);
                   th.Start(E);
               }
           }
           赶上(OPCException){}

       }
 

解决方案

您不应该尝试使用非公开的成员。如果在code笔者隐藏它,你应该假设:

  • 在他们不想让你访问它,至少没有直接。
  • 的实施可能会改变在任何时候,你的code应该继续工作。

您的可以的使用反射来访问非公共成员(假设在执行时相应的权限),但我会极力阻止你这样做,如果可能的。

how to get the value of non public member of the OPCGroup using OPCDA.NET in C#

       private void DataChangeHandler(object sender, DataChangeEventArgs e)
       {
           try
           {
               object obj = sender;
               OpcGroup grp = (OpcGroup)sender;//i want the OPCgroup non public member value

               if (InvokeRequired)
               {
                   BeginInvoke(new DataChangeEventHandler(DataChangeHandler), new object[] { sender, e });
                   //return;
               }
               if (parameter.X == 0)
               {
                   parameter.X = 1;
                   Thread th = new Thread(param.update);
                   th.Start(e);
               }
           }
           catch(OPCException ) { }

       }

解决方案

You shouldn't try to use non-public members. If the author of the code has hidden it, you should assume that:

  • They don't want you to access it, at least not directly.
  • The implementation could change at any time, and your code should keep working.

You can use reflection to access non-public members (assuming appropriate permissions at execution time) but I would highly discourage you from doing so if possible.

这篇关于获取非公开成员的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆