Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I stumbled upon this exact problem a few days ago. I couldn't find a reasonable explanation as to why it doesn't work.</p> <p>After spending long hours trying different workarounds, I think I finally found something that seems to work, and is not so dirty. What I did is implement the accessor to the collection in the container object as a method, instead of a property. This method receives one argument, the key. If the key is "missing" or null, then the method returns the collection (this handles expressions like "testRequest.QueryString.Count" in VbScript). Otherwise, the method returns the specific item from the collection.</p> <p>The dirty part with this approach is that this method returns an object (because sometimes the return reference is the collection, and sometimes an item of the collection), so using it from managed code needs castings everywhere. To avoid this, I created another property (this time a proper property) in the container that exposes the collection. This property is NOT exposed to COM. From C#/managed code I use this property, and from COM/VbScript/unmanaged code I use the method.</p> <p>Here is an implementation of the above workaround using the example of this thread:</p> <pre><code> [ComVisible(true)] [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface IRequest { IRequestDictionary ManagedQueryString { get; } // Property to use form managed code object QueryString(object key); // Property to use from COM or unmanaged code } [ComVisible(true)] [ClassInterface(ClassInterfaceType.None)] public class TestRequest : IRequest { private IRequestDictionary _queryString = new RequestDictionary(); public IRequestDictionary ManagedQueryString { get { return _queryString; } } public object QueryString(object key) { if (key is System.Reflection.Missing || key == null) return _queryString; else return _queryString[key]; } } [ComVisible(true)] [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface IRequestDictionary : IEnumerable { [DispId(0)] object this[object key] { [DispId(0)] get; [DispId(0)] set; } int Count { get; } } [ComVisible(true)] [ClassInterface(ClassInterfaceType.None)] public class RequestDictionary : IRequestDictionary { private Hashtable _dictionary = new Hashtable(); public object this[object key] { get { return _dictionary[key]; } set { _dictionary[key] = value; } } public int Count { get { return _dictionary.Count; } } #region IEnumerable Members public IEnumerator GetEnumerator() { throw new NotImplementedException(); } #endregion } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload