获取非公开成员的值成员

由网友(当你经历过大起大落之后,你会在一夜之间换一种性格,下面小编为)分享简介:如何开始使用OPCDA.NET在C#的OPCGroup的非公共成员的价值私人无效DataChangeHandler(对象发件人,DataChangeEventArgs E){尝试{obj对象=发送;OpcGroup GRP =(OpcGroup)发件人; //我想OPCgroup非公共成员值如果(InvokeRequi...

如何开始使用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应该继续工作。 qq群成员等级积分是怎么计算和获得累计的

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

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.

阅读全文

相关推荐

最新文章