请教微软的技术专家。一个完全不合理的问题!C#足够变态的问题!

colinfly 2002-05-26 04:49:56
当我这么写的时候
1:Type t = typeof (IOPCGroups);
2:int i;


3:i=Groups.Count;
系统会在运行到3:时出现
Object reference not set to an instance of an object.
异常。

而当我在第三句前加上i=(int)t.InvokeMember ("Count", BindingFlags.Public | BindingFlags.GetField | BindingFlags.GetProperty, null, Groups, new object [] {});
这句沃并未实际用到的话时,就正常了!
能告诉我为什么呢!

1:Type t = typeof (IOPCGroups);
2:int i;

21:i=(int)t.InvokeMember ("Count", BindingFlags.Public | BindingFlags.GetField | BindingFlags.GetProperty, null, Groups, new object [] {});

3:i=Groups.Count;


...全文
93 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
colinfly 2002-06-01
  • 打赏
  • 举报
回复
qqchen79(知秋一叶)
谢谢!
但这个在VB中正常,在c#的immediate窗口也正常。会是什么原因呢?

我是想等想你这样的高手,所以只给了1分!我都很看得起自己了,对这个问题竟然没办法!
qqchen79 2002-05-30
  • 打赏
  • 举报
回复
我试了一下,的确是这样。
但是这要设计到那个KEPServer实现,没办法跟进去调试了...
rengo 2002-05-30
  • 打赏
  • 举报
回复
qqchen79:这个程序是这样的,不过比较麻烦
先到:http://www.opcsource.com/download_opc.html
下载个"KEPServerEX Server Super Pack Setup Version 4.0 Full Install"14M
然后安装这个软件,新建一个C#项目,再引用COM里的Kepware OPC Automation 2.0
执行一下几句:
private OPCServer Server;
private OPCGroups Groups;

Server=new OPCServer();
Server.Connect("KEPware.KEPServerEx.V4",null);//连接OPC服务器,这时将启动OPC服务器(就是下载的那个软件)
Groups=Server.OPCGroups;
int i;
i=Groups.Count;//<-在这个就会出现conlinfly所所描述的问题


colinfly 2002-05-29
  • 打赏
  • 举报
回复
没人会吗?
qqchen79 2002-05-29
  • 打赏
  • 举报
回复
由完整的程序吗?
colinfly 2002-05-28
  • 打赏
  • 举报
回复
并非编译出错,是运行时出错。在没有加InvokeMember这句之前,运行到第三句(i=Groups.Count;)会出错.加了InvokeMember这句,运行到第三句(i=Groups.Count;)就没问题了。你们碰到过这种问题吗?
qqchen79 2002-05-27
  • 打赏
  • 举报
回复
Reflection是运行期间类型检查,所以没运行没错;而直接调用是编译期间类型检查,所以报错;仅此而已。
rengo 2002-05-27
  • 打赏
  • 举报
回复
但是加没加InvokeMember这句都可以通过编译
rengo 2002-05-27
  • 打赏
  • 举报
回复
我想colinfly (Colin)的意思是:没有加InvokeMember这句之前,运行到第三句(i=Groups.Count;)会出错.加了InvokeMember这句,运行到第三句就没问题了
colinfly 2002-05-26
  • 打赏
  • 举报
回复
不一定是漏洞,但肯定有一些小问题。
xdev 2002-05-26
  • 打赏
  • 举报
回复
t.InvokeMember 执行后的结果是Object

当然要cast 了
yinjialiang 2002-05-26
  • 打赏
  • 举报
回复
老兄,你写得乱七八糟的,不过若真是一个漏洞,赶快向微软反映吧,听说有奖的

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧