- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Tmiqpl
- {
- /// <summary>
- /// 创建 COM 对象
- /// </summary>
- public class CreateObject
- {
- /// <summary>
- /// COM 对象实例化
- /// </summary>
- /// <param name="progid">COM 对象名或 Class ID</param>
- public CreateObject(string progid)
- : this(progid, true)
- {
- }
- /// <summary>
- /// COM 对象实例化
- /// </summary>
- /// <param name="progid">COM ID</param>
- /// <param name="throwOnError">true 为引发一切可能发生的异常,false 忽略所有异常</param>
- public CreateObject(string progid, bool throwOnError)
- {
- otype = Type.GetTypeFromProgID(progid, throwOnError);
- obj = System.Activator.CreateInstance(otype);
- }
- /// <summary>
- /// 获取或设置参数值列表 (针对于调用方法)
- /// </summary>
- public object[] Params
- {
- get { return this.objparams; }
- set { this.objparams = value; }
- }
- /// <summary>
- /// 获取或设置要调用的 COM 对象内部方法名
- /// </summary>
- public string FunctionName
- {
- get { return this._functionname; }
- set { this._functionname = value; }
- }
- /// <summary>
- /// 调用预定义好的方法
- /// </summary>
- public object Call()
- {
- return otype.InvokeMember(this.FunctionName, System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Static| System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null, obj, this.Params);
- }
- /// <summary>
- /// 获取或设置已预定义好的属性的值 (针对于调用属性)
- /// </summary>
- public object Property
- {
- get
- {
- object[] _probj = null;
- return otype.InvokeMember(this.PropertyName, System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.IgnoreCase, null, obj, _probj);
- }
- set
- {
- object[] _proobj = new object[] { value };
- otype.InvokeMember(this.PropertyName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.SetProperty, null, obj, _proobj);
- }
- }
- /// <summary>
- /// 设置要调用的属性名称
- /// </summary>
- public string PropertyName
- {
- get
- {
- return this._propertyname;
- }
- set
- {
- this._propertyname = value;
- }
- }
- private string _propertyname;
- private string _functionname;
- //private string comid;
- private Type otype;
- private object obj;
- private object[] objparams;
- 例子
- / <summary>
- / 获取当前机器上 Access 主版本号
- / </summary>
- //public static string Version
- //{
- // get{
- // Tmiqpl.CreateObject _obj = new CreateObject("Access.Application");
- // _obj.PropertyName = "version";
- // return _obj.Property.ToString();
- // }
- //}
- }
- }
C# 与 COM 之间的沟通
最新推荐文章于 2024-07-06 10:49:22 发布