I need to write a C# application that uses some 3rd party, vendor
supplied COM objects. I do not have access to the COM object source
code.
I have had some success using the objects from VB.Net, and have used
them many times from VBA (Excel). I need to use C# as it is the
strategic platform for my client, in this case.
The problem is that C# does not seem to support late binding very
easily, and I need to late bind because as we need to be able to
upgrade/update the COM objects without breaking my application.
Here is an example of the COM object being used from VB.Net:
------------------------------------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim oSession As Object
Dim cf As Object
Dim Parms() As String = {"user", "password", "env"}
Dim transRef as Long
oSession = New MSRV.Session ' this is a COM obj I have referenced
oSession.logon(Parms(0), Parms(1), Parms(2))
cf = oSession.sessionItem("TRANS") ' TRANS is another COM
object I have referenced
cf.NewTrans()
cf.Trans.Properties.Item("Description").Value = "purchase"
cf.Trans.Properties.Item("Date").Value = "16-sep-2003"
cf.Trans.Properties.Item("Amount").Value = "1000"
' there are various other properties....
transRef = cf.SaveTrans()
Me.RichTextBox1.Text = "Trans No: " + CStr(transRef)
End Sub
------------------------------------------------------------------
As you can see, there is a master object which handles logins etc.,
and can "spawn" transaction input type objects via the SessionItem
method.
This all works just fine, VB.Net is late binding by default and I have
tested changing versions of the COM objects (where the members and
properties stay the same) but as I said, I want to do this in C#.
I used Lutz Roeders Reflector in an attempt to do a conversion from VB
to C#, but the decompilation shows that the assembly uses LateBinding
and related methods in the Microsoft.VisualBasic.CompilerServices
namespace. This namepsace does not appear to be available from C#.
Here is a sample from the decompilation (I added the fully qualified
namepsaces manually):
------------------------------------------------------------------
// this is the decompilation of the cf = oSession.sessionItem("TRANS")
// from the above VB example
array3 = new object[1];
array3[0] = "TRANS";
obj1 = System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue(Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(obj2,
null, "sessionItem", array3, null, null));
------------------------------------------------------------------
I have made many attempts at this problem using reflection and Type
objects etc, but have had no luck so far.
Can anyone please help? |