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

C#中实现VB中的CreateObject方法

xjtudll4年前 (2022-03-30)技术心得6420

经常看到有些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://xjtudll.cn/Exp/639/

标签: C#
分享给朋友:

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

SSCOM无法保存窗口数据到文本文件

SSCOM无法保存窗口数据到文本文件

使用环境: Win7 64bit SSCOM 3.2 问题: “保存窗口”功能无效,每次都是提示error 如下图所示: 原因: 相应的目录根本不存在,请看错误窗口。 目录SAVE2017/3实际上并不存在,软件也没有自动新建该目录 解决办法: 在SSCOM同级目录下,新建...

带锁存移位寄存器(verilog)

带锁存移位寄存器(verilog)

带锁存的移位寄存器 rclk——锁存时钟 sclk——移位时钟 din——输入数据 dout——输出数据 [shiftBitNumbers——移位寄存器位数 module shi...

SQL去掉小数点有效数字后的所有0

第一种方法 select cast(2.5000000000000   as  real) select cast(2   as  real) select cast(2.00000   as  r...

VS2010:此项目与Visual Studio的当前版本不兼容

VS2010:此项目与Visual Studio的当前版本不兼容

问题: 网上下载了一个C#工程,是用VS2013开发的,但是本地只有VS2010。打开后提示:此项目与Visual Studio的当前版本不兼容 解决办法: 1、用记事本打开解决方案文件“解决方案名.sln”,然后修改最上面两行为如下代码: Microsoft...

Android-如何关闭AlertDialog.Builder对话框

AlertDialog.Builder对话框没有类似finish()或者dismiss()这样的方法。 但是它的父类AlertDialog有dismiss方法,而且AlertDialog.Builder在.show()的时候会得到一个AlertDialog对象,我们就可以用dismiss方法将该Bu...

Android APK反编译

Android APK反编译

  反编译需要使用到以下两个软件 1、dex2jar http://code.google.com/p/dex2jar/downloads/list 2、jdgui http://code.google.com/p/innlab/downloads/list 反编译步骤如下: 1、将APK...

发表评论

访客

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