【问题标题】:How to pass parameters from Classic ASP to a com component如何将参数从经典 ASP 传递到 com 组件
【发布时间】:2011-09-18 12:39:13
【问题描述】:

我正在开发一个需要许多参数的 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 runtime error: Invalid procedure call or argument: 'checkObj' 。

是否可以将集合传递给 com 程序集?

也许问题是com组件接收到一个Scripting.Dictionary类型的对象,而不是我创建的抽象类,但是.net中不存在这样的东西?

【问题讨论】:

  • 猜测一下,您可能需要将object 参数转换为您正在使用的实际类型。 Object 确实没有 startDate 成员。
  • 是的,我创建了一个抽象类,现在它正在检查它并完美构建。在运行时,我现在收到错误 - Microsoft VBScript runtime error: Invalid procedure call or argument: 'checkObj' 。是否可以将集合传递给 com 程序集?
  • 我不相信 ASP.net 使用 COM。要从经典 ASP 与 .net 进行通信,我认为您需要设置一个 .net Web 服务。
  • 如果您从原始 C# 代码中删除 config.startDate 部分(将 config 作为对象),它是否可以正常运行而没有错误?如果是这样,请尝试使用反射在运行时动态获取属性。
  • 顺便说一句,当您发表评论以回复其他人的评论时,请使用@ 通知该人,例如@Shadow 会通知我,否则人们将看不到您的评论。

标签: c# asp.net com asp-classic


【解决方案1】:

您可以尝试在您的 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 组件中的事情变得更容易

【讨论】:

    【解决方案2】:

    我想做类似的事情,因此为 .NET DataRow 创建了一个包装类。如果需要,您可以使用 HasTable/Dictionairy/Other Custom Implementation 作为您的后备存储。

    我在包装器对象上使用索引器属性公开我的“属性”,因此在 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
    }
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-01
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 2011-09-29
      相关资源
      最近更新 更多