How to access an array of dynamic objects?

0

I need your help please,

I imported a .dll into my C# project and the .dll contains a bunch of classes, one of these classes is called PNR. This class is used to retrieve data from the system and it works as expected, but after I retrieve the data, it stores the data in the dynamic properties of this class. Two of these dynamic properties are called Header and NameElements which are also model classes in the .dll as seen below.

PNR class has the following dynamic properties:

[Guid("2B999FE4-68B2-11D0-9771-00008322DB13")]
[InterfaceType(2)]
[TypeLibType(4096)]
public interface IPNRObj
{
    [DispId(80)]
    int RetrieveCurrent(object pHostSession);

    //...

    [DispId(2)]
    dynamic NameElements { get; set; }
    [DispId(76)]
    dynamic Header { get; set; }

    //...
}

Header class has the following string properties:

[Guid("1ED01830-ED72-11D0-888E-000083262EA4")]
[InterfaceType(2)]
[TypeLibType(4096)]
public interface IPNRHeaderObj
{
    //...

    [DispId(1)]
    string Text { get; set; }
    [DispId(2)]
    int ElementNo { get; set; }
    [DispId(6)]
    string RecordLocator { get; set; }

    //...
}

NameElement class has the following string properties:

[Guid("7B12CE00-8B32-11D0-A516-444553540000")]
[InterfaceType(2)]
[TypeLibType(4096)]
public interface IPNRNameElementObj
{
    //...

    [DispId(1)]
    string Text { get; set; }
    [DispId(2)]
    int ElementNo { get; set; }
    [DispId(4)]
    string LastName { get; set; }

    //...
}

As you can see above, the PNR class properties are dynamic, so when I want to access the Header property of the PNR class for example, first I need to make an empty object of the Header class then assign it and access its data as seen below:

PNR pnr = new PNR(); // first create an empty object
pnr.RetrieveCurrent(oHost); // retrieve the data, now the pnr object contains the data

// pnr object contains .Header dynamic  property and this is how to access it
Header header = new Header(); // create an empty object of Header
header = pnr.Header; // assign Header dynamic property to the header object

Console.WriteLine(header.RecordLocator); // outputs the correct data

The above code works correctly with no problem, however I am facing an issue when trying to access data like the NameElements dynamic property of the PNR class. The PNR can have multiple names sometimes, so when I try the following code, it fails with an error:

// pnr object contains .NameElements dynamic property 
NameElement nameElement = new NameElement(); // create an empty object of NameElement
nameElement = pnr.NameElements; // outputs error number 1
nameElement = pnr.NameElements[0]; // outputs error number 2
Console.WriteLine(nameElement.LastName);

Error number 1 : System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'NameElement'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{7B12CE00-8B32-11D0-A516-444553540000}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'

Error number 2 : System.MissingMemberException: 'Error while invoking [PROPERTYGET, DISPID(0)].'

I don't know how to assign the nameElement the list of the values in the pnr object, and since it returns a dynamic property, I don't know how to deal with it.. I also tried assigning it to an array of NameElement, but I still get the same error.

NameElement [] nameElements;
nameElements = pnr.NameElements;
Console.WriteLine(nameElements[0].LastName);

Error number 3 : Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot implicitly convert type 'System.__ComObject' to 's1aPNR.NameElement[]''

Please help me out.

c#
arrays
dynamic
dll
comobject
asked on Stack Overflow Oct 15, 2018 by Abdullah Hashim • edited Oct 15, 2018 by dymanoid

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0