[英]How to pass parameters from Classic ASP to a com component
I am developing an asp.net component that requires many parameters. 我正在开发需要许多参数的asp.net组件。 It will be called from classic ASP. 它将从经典的ASP中调用。 I can of course pass in 10-20 parameters, but I'd love to be a bit tidier. 我当然可以传递10到20个参数,但是我想稍微整理一下。
I'm fairly confident I could pass in an array, but ideally I'd like to be able to pass in an object. 我非常有信心可以传递一个数组,但是理想情况下,我希望能够传递一个对象。
Is this possible? 这可能吗?
I decided to do a little test. 我决定做一点测试。 The Classic ASP: 经典ASP:
Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "startDate", startDate
objDictionary.Add "endDate", endDate
MyComponent.checkObj(objDictionary)
In my ASP.net component I have: 在我的ASP.net组件中,我有:
public string checkObj(object config)
{
return "StartDate is " + config.startDate;
}
Edit: 编辑:
I have progressed the issue so I'm changing this: I created an abstract class and now it's checking against that and building perfectly. 我已经解决了这个问题,所以我要对此进行更改:我创建了一个抽象类,现在它正在检查该类并完美构建。 At run time I am now getting and error - Microsoft VBScript runtime error: Invalid procedure call or argument: 'checkObj' . 在运行时,我现在遇到错误-Microsoft VBScript运行时错误:无效的过程调用或参数:'checkObj'。
Is it possible to pass a collection into a com assembly? 是否可以将集合传递到com程序集中?
Perhaps the problem is that the com component is receiving an object of type Scripting.Dictionary, and not the abstract class I created, but such a thing doesn't exist in .net? 也许问题在于com组件正在接收Scripting.Dictionary类型的对象,而不是我创建的抽象类,但是.net中不存在这种情况?
you could try using a .net object in your asp page like System.Collections.ArrayList or System.Collections.Hashtable instead of that dictionary... 您可以尝试在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")
%>
that should make things easier in your .net component 那应该使您的.net组件更容易
I wanted to do something similar and so created a wrapper class for .NET DataRow. 我想做类似的事情,因此为.NET DataRow创建了一个包装器类。 You could use a HasTable/Dictionairy/Other Custom Implementation as your backing store if you want. 如果需要,可以使用HasTable / Dictionairy / Other自定义实现作为后备存储。
I expose my "properties" using an Indexer Property on my wrapper object, so working with a property in asp-classic would look like this: 我在包装对象上使用Indexer属性公开了“属性”,因此使用asp-classic中的属性看起来像这样:
Dim lngCustomerId
lngCustomerID = CLng(objectWrapper("CustomerId"))
I expose my wrapper using a COM Registered .NET assembly. 我使用COM注册的.NET程序集公开包装器。 My wrapper inherits from DynamicObject and exposes the following by a COM visible interface: 我的包装器继承自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);
}
I would think that TryGetMember and TrySetMember will not be necessary for your needs. 我认为TryGetMember和TrySetMember对于您的需求将不是必需的。
My wrapper class implementation looks like this: 我的包装器类实现如下所示:
[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
}
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.