Providing indexer / default property via COM Interop

I am trying to write a component in C # that will be used by classic ASP, which allows me to access the component's index (as a default property).

For example:
Component C #:

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 user:

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

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

The goal is that I'm trying to create double tests for some built-in ASP objects, such as Request, that use collections using these properties by default (for example, Request.QueryString("key") ). Alternative suggestions are welcome.

Update: I asked the following question: Why is the indexer on my .NET component not always accessible from VBScript?

+4
collections c # vbscript asp-classic com-interop
source share
3 answers

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

+3
source share

Thanks to Rob Walker's hint, I got it by adding the following method and attribute to MyCollection:

 [DispId(0)] public string Item(string key) { return this[key]; } 

Edit: see this is the best solution that uses an index.

0
source share

Here is a better solution that uses an index rather than an Item method:

 public class MyCollection { private NameValueCollection _collection; [DispId(0)] public string this[string name] { get { return _collection[name]; } set { _collection[name] = value; } } } 

It can be used from ASP, for example:

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

Note. I could not get this to work before because I overloaded the indexer, this[string name] , with this[int index] .

0
source share

All Articles