繁体   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