本文介绍了通过COM Interop暴露索引器/默认属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C#中编写一个组件,由经典的ASP使用,允许我访问组件的索引器(也称为默认属性)。

I am attempting to write a component in C# to be consumed by classic ASP that allows me to access the indexer of the component (aka default property).

示例:

C#组件:

For example:
C# component:

public class MyCollection {
    public string this[string key] {
        get { /* return the value associated with key */ }
    }

    public void Add(string key, string value) {
        /* add a new element */
    }
}

ASP消费者:

Dim collection
Set collection = Server.CreateObject("MyCollection ")
Call collection.Add("key", "value")
Response.Write(collection("key")) ' should print "value"

有需要设置的属性,我需要实现一个接口,还是我需要做别的事情?或者这不可能通过COM Interop?

Is there an attribute I need to set, do I need to implement an interface or do I need to do something else? Or this not possible via COM Interop?

目的是,我试图创建测试双打一些内置的ASP对象,如请求,使用使用这些默认属性(例如 Request.QueryString(key))的集合。欢迎提出其他建议。

The purpose is that I am attempting to create test doubles for some of the built-in ASP objects such as Request, which make use of collections using these default properties (such as Request.QueryString("key")). Alternative suggestions are welcome.

更新:我问了一个后续问题:。

Try setting the DispId attribute of the property to be 0, as described here in the MSDN documentation.

这篇关于通过COM Interop暴露索引器/默认属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 21:05
查看更多