none
Interop System.Collections.IEnumerable of custom interface, C#, Delphi RRS feed

  • Question

  • Windows XP Pro, NET 3.5 sp1, VS 2008 sp1

    Hi, I have some troubles marshaling some IEnumerable to Delphi.

    I have the following method
    IEnumerable GetEnum()
    {
        yield return new MyObject();
        yield return new MyObject();
        yield break;
    }

    where MyObject implements properly ComVisible interface IMyInterface.

    At Delphi side this is IEnumVARIANT GetEnum();
    When I iterate through that enumerable:

    var
       var_item: OleVariant;
    
    while enum.Next(1, var_item, Fetched) = S_OK do
    begin
        if Fetched = 1 then
        begin
            if Supports(IDispatch(var_item), IMyInterface) then... // work with IMyInterface
        end
    end


    the var_item is always Unknown and the latter cast fails.

    If instead of returning MyObject I yield integers - they pass on the other side correctly and the var_item is not Unknown, I can get the integer OK.

    I know I can easily create some list interface for IMyInterface, be more type-safe and get this going but shouldn't this work the way I wrote it?
    Should I specify some [return : MarshalAsAttribute...] or something?

    Any help would be appreciated.

    Thanks.
    In fiery flight we would leave this hall...
    • Edited by Xaoc Friday, June 5, 2009 9:08 AM
    Friday, June 5, 2009 8:53 AM

Answers

  • VarSupports is the answer, my bad. I have forgotten this method.

    Thanks for staying with me.

    Have a nice day
    In fiery flight we would leave this hall...
    • Marked as answer by Xaoc Friday, June 5, 2009 11:35 AM
    Friday, June 5, 2009 11:34 AM

All replies

  • Why is IDispatch used in that code?  If an IDispatch cast works, the class probably only has [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] instead of a dual interface.
    Hans Passant.
    Friday, June 5, 2009 10:45 AM
    Moderator
  • No, the interface is attributed as Dual explicitly. Anyway, I cannot cast the yielded var_item (which is Unknown) to anything.

    // edit
    So IMyInterface(var_item) says 'invalid typecast'. Any ideas?
    In fiery flight we would leave this hall...
    Friday, June 5, 2009 10:58 AM
  • VarSupports is the answer, my bad. I have forgotten this method.

    Thanks for staying with me.

    Have a nice day
    In fiery flight we would leave this hall...
    • Marked as answer by Xaoc Friday, June 5, 2009 11:35 AM
    Friday, June 5, 2009 11:34 AM