All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
创建一个简单的Delphi程序
Asp.net,创建新网站http1.1,403,forbidden错误处理_.net资料_编程技术
动态网页技术PHP语法学习笔记_php资料_编程技术
黑客攻破SQL服务器系统的十种方法_[SQL,Server教程]
PHP教程:简单学习动态网页制作PHP中的Cookies_[PHP教程]
asp.net2.0,treeview控件用数据表做为数据源实现n级动态菜单_[Asp.Net教程]
详细介绍:Apache+PHP+MySQL配置攻略_[PHP教程]
用PHP实现登陆验证码(类似条行码状)_[PHP教程]
用php简单实现Search Engine Friendly的URL_[PHP教程]
Asp.net2.0实现Word转换Html,同时分享系列笑话_[Asp.Net教程]
Reading number is top 10 pictures
Extremely rare TianShan Mountains snow lotus1
这才是真正的人体艺术3
Beauty Sun Feifei
The dog buy the ham oneself
Players in the eyes of a perfect love2
粉红蕾丝的美女
Sell the barbecue as says father du breul4
Female star bikini
So beauty, will let you spray blood4
Absolutely shocked. National geographic 50 animal photographys1
Download software ranking
Tram sex maniac 2 (H) rar bag1
I'm come from Beijing1
Boxer's Top ten classic battle5
Tram sex maniac 2 (H) rar bag10
jdk1.6 for windows
Unix video tutorial11
Boxer Classic video3
Tram sex maniac 2 (H) rar bag19
linux高级编程
SP4 for SQL2000
aaa published in(发表于) 2013/12/17 7:46:18 Edit(编辑)
C#中实现VB中的CreateObject方法_.net资料_编程技术

C#中实现VB中的CreateObject方法_.net资料_编程技术

C#中实现VB中的CreateObject方法_.net资料_编程技术-你的首页-uuhomepage.com

  经常看到有些VB的例子中直接用个CreateObject就可调用系统功能(大多是COM对象),像用户设定,网络设定等等。虽然C#中可以通过使用VB的命名空间的方法来调用CreateObject函数,但是这样比较没什么用,因为生成的对象的所带有的方法都不能使用。C#中还可以直接用添加引用的方式来调用一些对象,前提是你知道该添加哪个引用。


  当我上网搜索,已经搜索到很多VB的成功用CreateObject调用的例子,C#的例子却很难找到的时候,就干脆用类似VB的方法算了,很简单。免得继续在网络中大海捞针了。  


  C#中类似 CreateObject 的方法就是 System.Activator.CreateInstance. 后续的对象函数的调用可以通过InvokeMember方法来实现。  


  如在VB中的源代码如下:


  这种方式叫Late-Bind,关于早期绑定和后期绑定的区别见 http://msdn2.microsoft.com/zh-cn/library/0tcf61s1(VS.80).aspx  


  Public Sub TestLateBind()
   Dim o As Object = CreateObject("SomeClass")
   o.SomeMethod(arg1, arg2)
   w = o.SomeFunction(arg1, arg2)
   w = o.SomeGet
   o.SomeSet = w
  End Sub


  转换成C#的代码如下所示:


  public void TestLateBind()
  {
   System.Type oType = System.Type.GetTypeFromProgID("SomeClass");
   object o = System.Activator.CreateInstance(oType);
   oType.InvokeMember("SomeMethod", System.Reflection.BindingFlags.InvokeMethod, null, o, new object[] {arg1, arg2});
   w = oType.InvokeMember("SomeFunction", System.Reflection.BindingFlags.InvokeMethod, null, o, new object[] {arg1, arg2});
   w = oType.InvokeMember("SomeGet", System.Reflection.BindingFlags.GetProperty, null, o, null);
   oType.InvokeMember("SomeSet", System.Reflection.BindingFlags.SetProperty, null, o, new object[] {w});
  }   


  里面有方法,属性的调用设定,很简单。  


  实际例子如下,调用Office功能的:  


   public void TestLateBind()
   {
   System.Type wordType = System.Type.GetTypeFromProgID( "Word.Application" );
   Object word = System.Activator.CreateInstance( wordType );
   wordType.InvokeMember( "Visible", BindingFlags.SetProperty, null, word, new Object[] { true } );
   Object documents = wordType.InvokeMember( "Documents", BindingFlags.GetProperty, null, word, null );
   Object document = documents.GetType().InvokeMember( "Add", BindingFlags.InvokeMethod, null, documents, null );
   }


  这种Activator.CreateInstance方法还可以用来创建实例,并调用某些接口方法。毕竟接口必须要实例才能调用。





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.