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

C#中实现VB中的CreateObject方法

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

经常看到有些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方法” 的相关文章

Word删除空格、空行、超链接宏

1、删除空格和空行 此宏的主要功能是删除空格,并将软回车替换为硬回车 Sub 删除空格和空行() ' ' 删除空格和空行 宏 ' '    '英文单词与英文单词之间保留一个半角空格,其他的所有空格均删除     myReplaceExecute Se...

office每次打开word都要配置进度 解决

office每次打开word都要配置进度 解决

Office2013每次打开都提示要配置,如图所示: 当然等待一段时间后,文件还是能打开的。 这个很烦。 解决方法: 1、点击“开始”——点击“运行”——输入“regedit”回...

DSDT常见的Warning

DSDT常见的Warning

黑苹果要想搞的好,少不了DSDT。DSDT在编译的时候,最常见的Warning有以下几种 1、Use of complier reserved name 如图。这种Warning的解决办法是:将“_T_2”改成“T_2” 2、Not all con...

金蝶K3 引出序时簿提示:cannot update database of object is read only

金蝶K3 引出序时簿提示:cannot update database of object is read only

金蝶K3引出序时簿提示:cannot update database of object is read only 解决办法: 更换引出路径...

ios7与ios8 注册本地通知

// IOS8 新系统需要使用新的代码    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)     {     &...

金蝶K3采购订单序时簿过滤条件的执行状态分别是什么意思?

【概述】 未完全到货:【数量】-【收料数量】>0的单据 未完全入库:【数量】-【入库数量】>0的单据 未完全开票:【数量】-【开票数量】>0的单据 未完全付款:【价税合计】-【付款关联金额】>0的单据 完全付款: 【价税合计】-【付款关联金额】=0的单据...

发表评论

访客

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