首页 新闻 搜索 专区 学院

关于C#调用com接口,给对象的域赋值

0
悬赏园豆:20 [待解决问题]

背景为做一个导入bug到qc系统中的小工具

VB样例为

Sub ConnectToQC(ByVal qcserver_txt, ByVal qcport_txt, ByVal qcusername_txt, ByVal qcpwd_txt, ByVal qcdomain_txt, ByValqcproj_txt)
 
Dim tdc1 As TDAPIOLELib.TDConnection
tdc1 =
New TDAPIOLELib.TDConnection
tdc1.InitConnectionEx(
"http://" & qcserver_txt & ":" & qcport_txt & "/qcbin")
tdc1.Login(qcusername_txt, qcpwd_txt)
tdc1.Connect(qcdomain_txt, qcproj_txt)
DimBugF
 
DimBugBg
BugF = tdc1.BugFactory()
BugBg = BugF.AddItem(System.
DBNull.Value)
BugBg.Summary =
"My Test Bug From VB App"
BugBg.Field(
"BG_DETECTION_DATE") = Today
BugBg.Field(
"BG_SEVERITY") = "2-Medium"
BugBg.Post()
 
 
If tdc1.Connected Then
MsgBox(
"You are connected to QC and created a Bug")
 
EndIf

改为C#版本

 TDConnection tdccon = new TDConnection();
                tdccon.InitConnectionEx(@"http://172.20.90.115:8899/qcbin");
                tdccon.Login("wenbinbin", "123456");
                tdccon.Connect("HTGL", "合同管理V5");
                
                dynamic BugF = tdccon.BugFactory;
                dynamic BugBg = BugF.AddItem(System.DBNull.Value);

                BugBg.Summary = "My Test Bug From C# App";
                BugBg.Priority = "1-低";

                WriteInMemory.WriteInMemory(ref BugBg, "BG_DETECTION_DATE", DateTime.Now.ToString());
                WriteInMemory.WriteInMemory(ref BugBg, "BG_SUBJECT", "18.纠纷管理");
                WriteInMemory.WriteInMemory(ref BugBg, "BG_RESPONSIBLE", "wenbinbin");
                WriteInMemory.WriteInMemory(ref BugBg, "BG_DETECTED_BY", "wenbinbin");
                WriteInMemory.WriteInMemory(ref BugBg, "BG_DESCRIPTION", "神马描述也木有");

                BugBg.Post();

                if (tdccon.Connected)
                {
                    MessageBox.Show("成功,Year!");
                }

 

其中创建bug对象使用下面代码报错

BugBg.Summary = "New defect";
                BugBg.Status = "New";
                BugBg.DetectedBy = "admin";
               
                //WriteInMemory(ref BugBg, "BG_DETECTION_DATE", "2008-11-21");
                BugBg.field("BG_DETECTION_DATE").VALUE = "2008-11-21";
                BugBg.field("BG_DETECTION_VERSION").VALUE = "系统版本";
                BugBg.field("BG_PRIORITY").VALUE = "服务级别";
                BugBg.Priority = "服务级别";
                BugBg.field("BG_PROJECT").VALUE = "上线阶段";
                BugBg.Project = "上线阶段";
                BugBg.field("BG_REPRODUCIBLE").VALUE = "N";
                BugBg.Reproducible = "N";
                BugBg.field("BG_RESPONSIBLE").VALUE = "admin";

 

具体错误信息为Cannot perform runtime binding on a null reference

无法直接用field赋值。必需使用

WriteInMemory.WriteInMemory(ref BugBg, "BG_DETECTION_DATE", DateTime.Now.ToString());
                WriteInMemory.WriteInMemory(ref BugBg, "BG_SUBJECT", "18.纠纷管理");
                WriteInMemory.WriteInMemory(ref BugBg, "BG_RESPONSIBLE", "wenbinbin");
                WriteInMemory.WriteInMemory(ref BugBg, "BG_DETECTED_BY", "wenbinbin");
                WriteInMemory.WriteInMemory(ref BugBg, "BG_DESCRIPTION", "神马描述也木有");

才不会报错。

WriteInMemory.WriteInMemory的具体VB实现

Public Class WriteInMemory
    Public Shared Sub WriteInMemory(ByRef obj As Object, ByVal field As String, ByVal value As String)
        Dim BugBg
        BugBg = obj
        BugBg.Field(field) = value
    End Sub
End Class

 

求大神解答下为毛用后面这个就不报错了

 

和和大虾的主页 和和大虾 | 初学一级 | 园豆:23
提问于:2014-04-22 17:25
< >
分享
所有回答(1)
0

可以写成类似:BugBg["BG_PRIORITY"] = "4-高";

江米条二号 | 园豆:202 (菜鸟二级) | 2014-10-28 23:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册