我想做類似的事情,因此爲.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);
}
我認爲TryGetMember和TrySetMember不會爲你的需求是必要的。
我的包裝類的實現看起來是這樣的:
[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
}
我希望這有助於。
根據猜測,您可能需要將'object'參數轉換爲您正在使用的實際類型。 'Object'確實沒有'startDate'成員。 – Oded
是的,我創建了一個抽象類,現在它正在檢查和完善。在運行時,我現在正在和錯誤 - Microsoft VBScript運行時錯誤:無效的過程調用或參數:'checkObj'。是否有可能將一個集合傳遞給com程序集? – iKode
我不相信ASP.net使用COM。從經典的ASP與.net通信我認爲你需要設置一個.net的Web服務。 – Dee