C#中实现VB中的CreateObject方法.
2009-11-26 17:35
441 查看
文章来源:/article/7032193.html
经常看到有些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方法还可以用来创建实例,并调用某些接口方法。毕竟接口必须要实例才能调用。
经常看到有些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方法还可以用来创建实例,并调用某些接口方法。毕竟接口必须要实例才能调用。
相关文章推荐
- c#实现和vb调用createobject()类似方法创建com对象,调用method和property
- C#中实现VB中的CreateObject方法.
- C#中实现VB中的CreateObject方法.
- C#中实现VB中的CreateObject方法
- C#中实现VB中的CreateObject方法.
- VB.NET,C#.NET调用Web Service,利用visual studio 的实现方法
- C#中调用VB中Inputbox类的实现方法
- C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现方法
- VB.NET,C#.NET调用Web Service,利用visual studio 的实现方法
- C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现方法
- 实现用VB.Net/(C#)开发K/3 BOS 插件的真正可行方法
- C#实现只运行单个实例应用程序的方法(使用VB.Net的IsSingleInstance)
- C#: 中文和UNICODE字符转换方法 及仿安居客地图实现。。。错误 GMap2未定义 解决办法 -VB 中文转为UNICODE字符
- C#三种模拟自动登录和提交POST信息的实现方法
- C#抽象工厂模式的几种实现方法及比较
- 不用AJAX实现前台JS调用后台C#方法(小技巧) 【改自Web.网际浪人.Blog】
- C#实现刷新桌面的方法
- JAVA通过反映方法实现C#的委托
- C#中实现屏蔽Ctrl+C的方法
- C#使用DataSet Datatable更新数据库的三种实现方法