云计算的特点
2010-09-09 13:03
190 查看
http://www.cnblogs.com/phytan/archive/2007/07/11/814175.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方法还可以用来创建实例,并调用某些接口方法。毕竟接口必须要实例才能调用。
可以参考我的另外一个随笔里面的源代码 http://www.cnblogs.com/phytan/archive/2007/07/11/814474.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方法还可以用来创建实例,并调用某些接口方法。毕竟接口必须要实例才能调用。
可以参考我的另外一个随笔里面的源代码 http://www.cnblogs.com/phytan/archive/2007/07/11/814474.html
相关文章推荐
- 【工业大数据】35页PPT讲解:工业大数据特点、价值及其计算
- SQL SERVER的排序规则 利用排序规则特点计算汉字笔划和取得拼音首字母 (转)
- Servlet访问路径的两种方式、Servlet生命周期特点、计算服务启动后的访问次数、Get请求、Post请求
- 简要描述一下Hadoop, Spark, MPI三种计算框架的特点以及分别适用于什么样的场景
- 用排序规则特点计算汉字笔划和取得拼音首字母
- 结课作业:云计算的的特点和在生活中的应用发展
- 云计算的关键特点及挑战
- 云计算的定义和特点
- 云计算入门--基本概念和特点
- 利用排序规则特点计算汉字笔划和取得拼音首字母(转)
- 常见分布式计算框架特点
- 云计算特点
- C语言循环中体现的计算机特有的算法(计算机特点:不怕计算繁琐)
- 云计算的关键特点及挑战
- Hadoop, Spark, MPI三种计算框架的特点以及分别适用于什么样的场景
- 云计算的特点
- 利用排序规则特点计算汉字笔划和取得拼音首字母
- 2018最新深度学习与神经网络的计算特点与硬件配置分析
- 了解switch的选择器的特点并用switch计算指定的年月日是本年的第几天
- Windows Azure Platform (三)云计算的特点