[U8开发] com对象调用类 DxComObject

  • 避免复杂的com引用
 1 using System;
 2 using System.Globalization;
 3 using System.Reflection;
 4 using System.Runtime.InteropServices;
 5 
 6 public class DxComObject : IDisposable
 7 {
 8     private Type _ObjType;
 9 
10     public object ComInstance;
11 
12     public Type ComType
13     {
14         get
15         {
16             return _ObjType;
17         }
18     }
19 
20     public object this[string propName]
21     {
22         get
23         {
24             return _ObjType.InvokeMember(propName, BindingFlags.GetProperty, null, ComInstance, null);
25         }
26         set
27         {
28             _ObjType.InvokeMember(propName, BindingFlags.SetProperty, null, ComInstance, new object[1]
29             {
30                 value
31             });
32         }
33     }
34 
35     public object this[string filedName, BindingFlags flags]
36     {
37         get
38         {
39             bool flag = 0 == 0;
40             return _ObjType.InvokeMember(filedName, flags, null, ComInstance, null);
41         }
42         set
43         {
44             bool flag = 0 == 0;
45             _ObjType.InvokeMember(filedName, flags, null, ComInstance, new object[1]
46             {
47                 value
48             });
49         }
50     }
51 
52     public DxComObject(string ComName)
53     {
54         _ObjType = Type.GetTypeFromProgID(ComName, false);
55         if (_ObjType == null)
56         {
57             throw new Exception("指定的COM对象名称无效");
58         }
59         ComInstance = Activator.CreateInstance(_ObjType);
60     }
61 
62     public object DoMethod(string MethodName, object[] args)
63     {
64         return ComType.InvokeMember(MethodName, BindingFlags.InvokeMethod, null, ComInstance, args);
65     }
66 
67     public object DoMethod(string MethodName, object[] args, object[] paramMods)
68     {
69         ParameterModifier parameterModifier = new ParameterModifier(paramMods.Length);
70         for (int i = 0; i < paramMods.Length; i++)
71         {
72             parameterModifier[i] = Convert.ToBoolean(paramMods[i]);
73         }
74         ParameterModifier[] modifiers = new ParameterModifier[1]
75         {
76             parameterModifier
77         };
78         return ComType.InvokeMember(MethodName, BindingFlags.InvokeMethod, null, ComInstance, args, modifiers, CultureInfo.CurrentCulture, null);
79     }
80 
81     public void Dispose()
82     {
83         Marshal.ReleaseComObject(ComInstance);
84     }
85 }
  • 调用1- 登录
 1          DxComObject dxComObject = new DxComObject("U8Login.clsLogin");
 2         array = new object[1]
 3         {
 4             LoginString
 5         };
 6         if (!(bool)dxComObject.DoMethod("ConstructLogin", array))
 7         {
 8             throw new Exception("U8登录信息错误!");
 9         }
10 
11          array = new object[8]
12         {
13             "ST",
14             errMsg,
15             errMsg,
16             errMsg,
17             errMsg,
18             errMsg,
19             errMsg,
20             errMsg
21         };
22         if (!(bool)dxComObject.DoMethod("Login", array))
23         {
24             throw new Exception(string.Format("登录{0}错误!{1}", "ST", dxComObject["ShareString"].ToString()));
25         }
26 
27          string text = dxComObject["UfDbName"].ToString();
28         string DBUser = string.Empty;
29         string DBPassword = string.Empty;
30         string text2 = SplitDBInfo(text, out DBUser, out DBPassword);
  •   调用2- CO调用
DxComObject dxComObject2 = new DxComObject("USERPCO.VoucherCO");
array = new object[2]
{
dxComObject.ComInstance,
 errMsg
 };
 if (!(bool)dxComObject2.DoMethod("IniLogin", array))
{
 throw new Exception("初始化CO接口失败!" + array.GetValue(1).ToString());
}

...

            if (!(bool)dxComObject2.DoMethod("Insert", array, paramMods))
            {
                throw new Exception("单据保存失败!" + array.GetValue(4).ToString());
            }
            empty = array.GetValue(6).ToString();
            errMsg = "入库单保存成功!";

用友二开合作

原文地址:https://www.cnblogs.com/lothar/p/13176291.html