2013年4月5日 星期五
如何用C# 寫成 COM+ 物件後然後使用 asp 調用 custom class Array(PHP就不用和麼麻煩) => Retrieve Custom class array from COM object using Classic ASP
我現在在維護一些傳統 asp 網頁,裡面會使用到我自己寫的 COM (C#),然後把他安裝到元件服務。
我所需要的回傳物件 data 格式為下
public class eTicket_Info : ServicedComponent, IeTicket_Info
{
public int OrderBulkNo { get; set; }
public string Pno { get; set; }
public string agentId { get; set; }
public string rloc { get; set; }
public DateTime rlocCreateDate { get; set; }
public string OfficeID { get; set; }
public string SessionKey { get; set; }
public string IATA_Number { get { return "3XXXX615"; } }
public string Issuing_Agent { get { return "AXXX"; } }
public string Issuing_Agent_Location { get { return "XXX"; } }
public object flight_Detail_Info {
[return: MarshalAs(UnmanagedType.Struct, SafeArraySubType = VarEnum.VT_ARRAY)] get;
set; } //Flight_Detail_Info
public object members_simple_Info {
[return: MarshalAs(UnmanagedType.Struct, SafeArraySubType = VarEnum.VT_ARRAY)] get;
set; } // Members_simple_Info
}
嚐試過很多次,總算找到對的方法,因為不知如何調用 members_simple_Info [],最後發現是必須使用。
[return: MarshalAs(UnmanagedType.Struct, SafeArraySubType = VarEnum.VT_ARRAY)] get
然後其實回傳的是 array。而且他的 Type 請放 object,VB喜歡這樣,如果傳回他原本的 Type,會有 error
在 Implementation class 裡需要這樣寫才才行
var _fdCount = _fd.Count();
if (_fdCount > 0)
{
data.flight_Detail_Info = new Flight_Detail_Info[_fdCount];
data.flight_Detail_Info = _fd.Cast<object>().ToArray();
}
else
data.flight_Detail_Info = null;
在 asp 裡請這樣寫,重點是,這樣寫後,我可以直接把 array 指定給參數(以下藍字)
Dim data
Set data = PNRObj.retrievePNR((PNR), (OrderBulkNo), (O_autoindex), (ms_sno) )
response.write "data.rloc(8):" & data.Issuing_Agent
'Set paxs = data.members_simple_Info
if IsArray(data.members_simple_Info) then Response.Write("IsArray") & "<br />"
Response.Write( "VarType(data.members_simple_Info:" & VarType(data.members_simple_Info)) & "<br />"
Response.Write("UBound:" & UBound(data.members_simple_Info)) & "<br />"
Response.Write("VarType(data.flight_Detail_Info):" & VarType(data.flight_Detail_Info)) & "<br />"
Response.Write("UBound:" & UBound(data.flight_Detail_Info)) & "<br />"
if UBound(data.members_simple_Info)>=0 then
paxs = data.members_simple_Info
For Each pax In paxs
response.write pax.firstname & "<br />"
Next
end if
if UBound(data.flight_Detail_Info)>=0 then
flights = data.flight_Detail_Info()
For Each flight In flights
response.write flight.fltConxFrom & "<br />"
Next
end if
大功告成
Improvement:
看可不可以簡化 C# 那裡的 code, 再跟大家做報告
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言