当前位置:首页 > 技术心得 > 正文内容

C#中实现VB中的CreateObject方法

xjtudll3年前 (2022-03-30)技术心得3290

经常看到有些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方法还可以用来创建实例,并调用某些接口方法。毕竟接口必须要实例才能调用。 
可以参考我的另外一个随笔里面的源代码
http://www.cnblogs.com/phytan/archive/2007/07/11/814474.html

扫描二维码推送至手机访问。

版权声明:本文由鸟的天空发布,如需转载请注明出处。

本文链接:http://www.xjtudll.cn/Exp/639/

标签: C#
分享给朋友:

“C#中实现VB中的CreateObject方法” 的相关文章

Proteus error:

Proteus error:"probe object xxx is ambigously placed"

Proteus仿真时,在添加电流探针以后,提示error "probe object xxx is ambigously placed" 如图所示: 原因: 电流探针仿真的时候那个电流的符号要和导线的方向一致 出现问题就是因为这个 解决办法: 改变电流探针方向,使之与导线方向...

Protel99SE高级规则设定

Protel99SE高级规则设定

1、Protel99SE高级间距规则 Protel99SE间距规则是在Rules里面设置的,通常我们只是设置整个PCB的间距,实际上我们可以单独设置各类间距,比如覆铜间距,过孔与过孔间距,焊盘到焊盘间距,焊盘到过孔间距等等。 Design->Rules->Routing->Clea...

服务端把客户端几次发的数据一起接受了,是怎么回事?

服务端把客户端几次发的数据一起接受了,是怎么回事?

  客户端是android,服务端是c#,手机监听手指一动就把手指所在的那个点的位置发给服务端,服务端死循环接收,android可以保证一次是给服务端发一条数据,但是服务端把几次发的数据一起接受了 原因: 因为TCP是流式数据,没有次的概念。看题主的数据,结构本身比较简单,可以试试利用...

Quartus II Warning:can't generate programming files because you are currently using the Quartus II softwore in Evaluation Node

开发环境: Quartus II 11.0 问题: 代码编译通过,但是下载到CPLD,没有任何现象 且有warning 如下: can't generate programming files because you are currently using the Quartus II softwo...

紫外线擦除程序

无意中得知,tenx十速的TM89系列OTP单片机可以用紫外线擦除程序,这下让我们在初期调试程序的时候节省了不少。以前都是用一个demo板,将程序烧到demo板的EEPROM里,来仿真效果。现在就不用这样折腾了,直接烧录进去,觉得程序有bug,就用紫外线擦除程序,重新烧录。 网上搜了搜关于紫外线擦...

PlatformIO串口无输出

PlatformIO串口无输出

问题:同样的串口打印代码,在Arduino IDE里可以正常输出,但是在PlatformIO里看不到任何输出硬件:ESP32-S3 Camera解决方法:This is because DTR and RTS both are connected to the RESET pin and GPIO...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。