簡體   English   中英

如何將參數從經典ASP傳遞到com組件

[英]How to pass parameters from Classic ASP to a com component

我正在開發需要許多參數的asp.net組件。 它將從經典的ASP中調用。 我當然可以傳遞10到20個參數,但是我想稍微整理一下。

我非常有信心可以傳遞一個數組,但是理想情況下,我希望能夠傳遞一個對象。

這可能嗎?

我決定做一點測試。 經典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中不存在這種情況?

您可以嘗試在ASP頁面中使用.net對象,例如System.Collections.ArrayList或System.Collections.Hashtable,而不是該字典...

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

那應該使您的.net組件更容易

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

我在包裝對象上使用Indexer屬性公開了“屬性”,因此使用asp-classic中的屬性看起來像這樣:

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
}

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM