2011-06-20 76 views
0

我正在開發一個需要許多參數的asp.net組件。它將從傳統的ASP中調用。我當然可以傳遞10-20個參數,但是我希望能夠更加整潔。如何將參數從傳統ASP傳遞到com組件

我很自信我可以傳入一個數組,但理想情況下我希望能夠傳入一個對象。

這可能嗎?

我決定做一個小測試。 經典ASP:

Dim objDictionary 
Set objDictionary = CreateObject("Scripting.Dictionary") 

objDictionary.Add "startDate", startDate 
objDictionary.Add "endDate", endDate 

MyComponent.checkObj(objDictionary) 

在我的ASP.net組件我有:

public string checkObj(object config) 
    { 
     return "StartDate is " + config.startDate; 
    } 

編輯:

我進步了這個問題,使我改變了這一點:我創建了一個 抽象類,現在它正在檢查和完善。在運行時,我現在正在和錯誤 - Microsoft VBScript運行時錯誤:無效的過程調用或參數:'checkObj'。

是否可以將集合傳遞給com程序集?

也許問題是com組件正在接收Scripting.Dictionary類型的對象,而不是我創建的抽象類,但是.net中不存在這樣的事情?

+0

根據猜測,您可能需要將'object'參數轉換爲您正在使用的實際類型。 'Object'確實沒有'startDate'成員。 – Oded

+0

是的,我創建了一個抽象類,現在它正在檢查和完善。在運行時,我現在正在和錯誤 - Microsoft VBScript運行時錯誤:無效的過程調用或參數:'checkObj'。是否有可能將一個集合傳遞給com程序集? – iKode

+0

我不相信ASP.net使用COM。從經典的ASP與.net通信我認爲你需要設置一個.net的Web服務。 – Dee

回答

1

你可以嘗試在你的ASP頁面像System.Collections.ArrayList或System.Collections.Hashtable,而不是該字典使用.NET對象...

<%@ LANGUAGE="VBSCRIPT" %> 
<% 
dim netObj  
set netObj = server.createobject("System.Collections.Hashtable") 
' or: 
'set netObj = server.createobject("System.Collections.ArrayList") 
%> 

,應該讓事情在你的更容易。網絡組件

0

我想做類似的事情,因此爲.NET DataRow創建了一個包裝類。如果需要,可以使用HasTable/Dictionairy /其他自定義實現作爲後備存儲。

我暴露我的「屬性」我的包裝對象上使用索引屬性,所以在ASP經典與屬性的工作應該是這樣的:

Dim lngCustomerId 
lngCustomerID = CLng(objectWrapper("CustomerId")) 

我使用COM註冊.NET暴露我的包裝部件。我的包裝從DynamicObject繼承和一個COM可視界面公開以下:

[ComVisible(true)] 
[Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")] 
[InterfaceType(ComInterfaceType.InterfaceIsDual)] 
public interface IDynamicModel 
{ 
    dynamic this[string propertyName] { get; set; } 
    bool TryGetMember(GetMemberBinder binder, out object result); 
    bool TrySetMember(SetMemberBinder binder, object value); 
} 

我認爲TryGetMemberTrySetMember不會爲你的需求是必要的。

我的包裝類的實現看起來是這樣的:

[ComVisible(true)] 
[Guid("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY")] 
[ProgId("COMLIB.DynamicModel")] 
[ClassInterface(ClassInterfaceType.None)] 
public sealed class DynamicModel : DynamicObject, IDynamicModel 
{ 
    #region indexer 

    public dynamic this[string propertyName] 
    { 
     get 
     { 
      dynamic propertyValue; 
      if (TryGetMember(propertyName, out propertyValue) != true) 
      { 
       propertyValue = null; 
      } 
      return propertyValue; 
     } 
     set 
     { 
      if (TrySetMember(propertyName, value) != true) 
      { 
       throw new ArgumentException("Cannot set property value"); 
      } 
     } 
    } 

    #endregion indexer 


    #region Fields 

    private DataRow dataRow; 

    #endregion Fields 


    #region Properties 

    public dynamic GetAsDynamic { get { return this; } } 

    #endregion Properties 


    #region CTOR Methods 

    public DynamicModel() 
     : base() 
    { 
     DataTable dataTable = new DataTable(); 
     this.dataRow = dataTable.NewRow(); 
    } 

    public DynamicModel(DataRow dataRow) 
     : base() 
    { 
     this.dataRow = dataRow; 
    } 

    #endregion CTOR Methods 


    #region Dynamic Object Member Overrides 

    public override bool TryGetMember(GetMemberBinder binder, out object columnValue) 
    { 
     bool result = false; 
     columnValue = null; 
     result = TryGetMember(binder.Name, out columnValue); 
     return result; 
    } 

    public override bool TrySetMember(SetMemberBinder binder, object value) 
    { 
     bool result = false; 
     result = TrySetMember(binder.Name, value); 
     return result; 
    } 

    #endregion Dynamic Object Member Overrides 


    #region Operations 

    public bool TryGetMember(string columnName, out dynamic columnValue) 
    { 
     bool result = false; 
     columnValue = null; 
     if (dataRow != null && dataRow.Table.Columns.Contains(columnName)) 
     { 
      columnValue = dataRow[columnName]; 
      result = true; 
     } 
     return result; 
    } 

    public bool TrySetMember(string columnName, dynamic columnValue) 
    { 
     bool result = false; 
     if (dataRow != null && dataRow.Table.Columns.Contains(columnName) == true) 
     { 
      dataRow[columnName] = columnValue; 
      result = true; 
     } 
     else 
     { 
      Type type = columnValue.GetType(); 
      DataColumn dataColumn = new DataColumn(columnName, type); 
      result = TrySetDataColumn(dataColumn, type, columnValue); 
     } 
     return result; 
    } 

    private bool TrySetDataColumn(DataColumn dataColumn, Type type, object value) 
    { 
     bool result = false; 
     dataRow.Table.Columns.Add(dataColumn); 
     result = TrySetMember(dataColumn.ColumnName, value); 
     return result; 
    } 

    #endregion Operations 
} 

我希望這有助於。